home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / elispman.lha / elispman / makeinfo.c < prev    next >
C/C++ Source or Header  |  1993-06-01  |  184KB  |  7,550 lines

  1. /* Makeinfo -- convert texinfo format files into info files.
  2.  
  3.    Copyright (C) 1987, 1992, 1993 Free Software Foundation, Inc.
  4.  
  5.    This file is part of GNU Info.
  6.  
  7.    Makeinfo is distributed in the hope that it will be useful,
  8.    but WITHOUT ANY WARRANTY.  No author or distributor accepts
  9.    responsibility to anyone for the consequences of using it or for
  10.    whether it serves any particular purpose or works at all, unless he
  11.    says so in writing.  Refer to the GNU Emacs General Public License
  12.    for full details.
  13.  
  14.    Everyone is granted permission to copy, modify and redistribute
  15.    Makeinfo, but only under the conditions described in the GNU Emacs
  16.    General Public License.   A copy of this license is supposed to
  17.    have been given to you along with GNU Emacs so you can know your
  18.    rights and responsibilities.  It should be in a file named COPYING.
  19.    Among other things, the copyright notice and this notice must be
  20.    preserved on all copies.  */
  21.  
  22. /* This is Makeinfo version 1.55.  If you change the version number of
  23.    Makeinfo, please change it here and at the lines reading:
  24.  
  25.     int major_version = 1;
  26.     int minor_version = 55;
  27.  
  28.    in the code below.
  29.  
  30.    Makeinfo is authored by Brian Fox (bfox@ai.mit.edu). */
  31.  
  32. /* You can change some of the behaviour of Makeinfo by changing the
  33.    following defines: */
  34.  
  35. /* Define INDENT_PARAGRAPHS_IN_TABLE if you want the paragraphs which
  36.    appear within an @table, @ftable, or @itemize environment to have
  37.    standard paragraph indentation.  Without this, such paragraphs have
  38.    no starting indentation. */
  39. /* #define INDENT_PARAGRAPHS_IN_TABLE */
  40.  
  41. /* Define DEFAULT_INDENTATION_INCREMENT as an integer which is the amount
  42.    that @example should increase indentation by.  This incremement is used
  43.    for all insertions which indent the enclosed text. */
  44. #define DEFAULT_INDENTATION_INCREMENT 5
  45.  
  46. /* Define PARAGRAPH_START_INDENT to be the amount of indentation that
  47.    the first lines of paragraphs receive by default, where no other
  48.    value has been specified.  Users can change this value on the command
  49.    line, with the +paragraph-indent option, or within the texinfo file,
  50.    with the @paragraphindent command. */
  51. #define PARAGRAPH_START_INDENT 3
  52.  
  53. /* Define DEFAULT_PARAGRAPH_SPACING as the number of blank lines that you
  54.    wish to appear between paragraphs.  A value of 1 creates a single blank
  55.    line between paragraphs.  Paragraphs are defined by 2 or more consecutive
  56.    newlines in the input file (i.e., one or more blank lines). */
  57. #define DEFAULT_PARAGRAPH_SPACING 1
  58.  
  59. /* **************************************************************** */
  60. /*                                    */
  61. /*            Include File Declarations               */
  62. /*                                    */
  63. /* **************************************************************** */
  64.  
  65. /* Indent #pragma so that older Cpp's don't try to parse it. */
  66. #if defined (_AIX)
  67.  # pragma alloca
  68. #endif /* _AIX */
  69.  
  70. #include <stdio.h>
  71. #include <sys/types.h>
  72. #include <ctype.h>
  73. #include <sys/stat.h>
  74. #include <ctype.h>
  75. #include <pwd.h>
  76. #include <errno.h>
  77. #if defined (HAVE_VARARGS_H)
  78. #include <varargs.h>
  79. #endif /* HAVE_VARARGS_H */
  80. #include "getopt.h"
  81.  
  82. #if defined (VMS)
  83. #include <perror.h>
  84. #endif
  85.  
  86. #if defined (HAVE_STRING_H)
  87. #include <string.h>
  88. #else
  89. #include <strings.h>
  90. #endif /* !HAVE_STRING_H */
  91.  
  92. #if defined (TM_IN_SYS_TIME)
  93. #include <sys/time.h>
  94. #else
  95. #include <time.h>
  96. #endif /* !TM_IN_SYS_TIME */
  97.  
  98. #if defined (HAVE_SYS_FCNTL_H)
  99. #include <sys/fcntl.h>
  100. #else
  101. #include <fcntl.h>
  102. #endif /* !HAVE_SYS_FCNTL_H */
  103.  
  104. #include <sys/file.h>
  105.  
  106. #if defined (__GNUC__)
  107. #define alloca __builtin_alloca
  108. #else
  109. #if defined(HAVE_ALLOCA_H)
  110. #include <alloca.h>
  111. #else /* !HAVE_ALLOCA_H */
  112. #if !defined (_AIX)
  113. extern char *alloca ();
  114. #endif /* !_AIX */
  115. #endif /* !HAVE_ALLOCA_H */
  116. #endif /* !__GNUC__ */
  117.  
  118. void *xmalloc (), *xrealloc ();
  119. static void isolate_nodename ();
  120.  
  121. /* Non-zero means that we are currently hacking the insides of an
  122.    insertion which would use a fixed width font. */
  123. static int in_fixed_width_font = 0;
  124.  
  125. /* Non-zero means that start_paragraph () MUST be called before we pay
  126.    any attention to close_paragraph () calls. */
  127. int must_start_paragraph = 0;
  128.  
  129. /* Some systems don't declare this function in pwd.h. */
  130. struct passwd *getpwnam ();
  131.  
  132.  
  133. /* **************************************************************** */
  134. /*                                    */
  135. /*                  Global Defines                  */
  136. /*                                    */
  137. /* **************************************************************** */
  138.  
  139. /* Error levels */
  140. #define NO_ERROR 0
  141. #define SYNTAX     2
  142. #define FATAL     4
  143.  
  144. /* How to allocate permanent storage for STRING. */
  145. #define savestring(x) \
  146.   ((char *)strcpy ((char *)xmalloc (1 + ((x) ? strlen (x) : 0)), \
  147.            (x) ? (x) : ""))
  148.  
  149. /* C's standard macros don't check to make sure that the characters being
  150.    changed are within range.  So I have to check explicitly. */
  151.  
  152. /* GNU Library doesn't have toupper().  Until GNU gets this fixed, I will
  153.    have to do it. */
  154. #ifndef toupper
  155. #define toupper(c) ((c) - 32)
  156. #endif
  157.  
  158. #define coerce_to_upper(c) ((islower(c) ? toupper(c) : (c)))
  159. #define coerce_to_lower(c) ((isupper(c) ? tolower(c) : (c)))
  160.  
  161. #define control_character_bit 0x40 /* %01000000, must be off. */
  162. #define meta_character_bit 0x080/* %10000000, must be on.  */
  163. #define CTL(c) ((c) & (~control_character_bit))
  164. #define UNCTL(c) coerce_to_upper(((c)|control_character_bit))
  165. #define META(c) ((c) | (meta_character_bit))
  166. #define UNMETA(c) ((c) & (~meta_character_bit))
  167.  
  168. #define whitespace(c) (((c) == '\t') || ((c) == ' '))
  169. #define sentence_ender(c) ((c) == '.' || (c) == '?' || (c) == '!')
  170. #define cr_or_whitespace(c) (((c) == '\t') || ((c) == ' ') || ((c) == '\n'))
  171.  
  172. #ifndef isletter
  173. #define isletter(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
  174. #endif
  175.  
  176. #ifndef isupper
  177. #define isupper(c) ((c) >= 'A' && (c) <= 'Z')
  178. #endif
  179.  
  180. #ifndef isdigit
  181. #define isdigit(c)  ((c) >= '0' && (c) <= '9')
  182. #endif
  183.  
  184. #ifndef digit_value
  185. #define digit_value(c) ((c) - '0')
  186. #endif
  187.  
  188. #define member(c, s) (strchr (s, c) != NULL)
  189.  
  190. #define COMMAND_PREFIX '@'
  191.  
  192. /* Stuff for splitting large files. */
  193. #define SPLIT_SIZE_THRESHOLD 70000  /* What's good enough for Stallman... */
  194. #define DEFAULT_SPLIT_SIZE 50000    /* Is probably good enough for me. */
  195. int splitting = 1;            /* Always true for now. */
  196.  
  197. typedef int FUNCTION ();    /* So I can say FUNCTION *foo; */
  198.  
  199.  
  200. /* **************************************************************** */
  201. /*                                    */
  202. /*                Global Variables                */
  203. /*                                    */
  204. /* **************************************************************** */
  205.  
  206. /* Global pointer to argv[0]. */
  207. char *progname;
  208.  
  209. /* The current input file state. */
  210. char *input_filename;
  211. char *input_text;
  212. int size_of_input_text;
  213. int input_text_offset;
  214. int line_number;
  215.  
  216. #define curchar() input_text[input_text_offset]
  217.  
  218. #define command_char(c) ((!whitespace(c)) && \
  219.              ((c) != '\n') && \
  220.              ((c) != '{') && \
  221.              ((c) != '}') && \
  222.              ((c) != '='))
  223.  
  224. #define skip_whitespace() while (input_text_offset != size_of_input_text \
  225.                  && whitespace(curchar()))\
  226.   input_text_offset++
  227.  
  228. /* Return non-zero if STRING is the text at input_text + input_text_offset,
  229.    else zero. */
  230. #define looking_at(string) \
  231.   (strncmp (input_text + input_text_offset, string, strlen (string)) == 0)
  232.  
  233. /* And writing to the output. */
  234.  
  235. /* The output file name. */
  236. char *output_filename = (char *)NULL;
  237. char *pretty_output_filename;
  238.  
  239. /* Name of the output file that the user elected to pass on the command line.
  240.    Such a name overrides any name found with the @setfilename command. */
  241. char *command_output_filename = (char *)NULL;
  242.  
  243. /* A colon separated list of directories to search for files included
  244.    with @include.  This can be controlled with the `-I' option to makeinfo. */
  245. char *include_files_path = (char *)NULL;
  246.  
  247. /* Current output stream. */
  248. FILE *output_stream;
  249.  
  250. /* Position in the output file. */
  251. int output_position;
  252.  
  253. /* Output paragraph buffer. */
  254. unsigned char *output_paragraph;
  255.  
  256. /* Offset into OUTPUT_PARAGRAPH. */
  257. int output_paragraph_offset;
  258.  
  259. /* The output paragraph "cursor" horizontal position. */
  260. int output_column = 0;
  261.  
  262. /* Non-zero means output_paragraph contains text. */
  263. int paragraph_is_open = 0;
  264.  
  265. #define INITIAL_PARAGRAPH_SPACE 5000
  266. int paragraph_buffer_len = INITIAL_PARAGRAPH_SPACE;
  267.  
  268. /* Filling.. */
  269. /* Non-zero indicates that filling will take place on long lines. */
  270. int filling_enabled = 1;
  271.  
  272. /* Non-zero means that words are not to be split, even in long lines.  This
  273.    gets changed for cm_w (). */
  274. int non_splitting_words = 0;
  275.  
  276. /* Non-zero indicates that filling a line also indents the new line. */
  277. int indented_fill = 0;
  278.  
  279. /* The column at which long lines are broken. */
  280. int fill_column = 72;
  281.  
  282. /* The amount of indentation to apply at the start of each line. */
  283. int current_indent = 0;
  284.  
  285. /* The amount of indentation to add at the starts of paragraphs.
  286.    0 means don't change existing indentation at paragraph starts.
  287.    > 0 is amount to indent new paragraphs by.
  288.    < 0 means indent to column zero by removing indentation if necessary.
  289.  
  290.    This is normally zero, but some people prefer paragraph starts to be
  291.    somewhat more indented than paragraph bodies.  A pretty value for
  292.    this is 3. */
  293. int paragraph_start_indent = PARAGRAPH_START_INDENT;
  294.  
  295. /* Non-zero means that the use of paragraph_start_indent is inhibited.
  296.    @example uses this to line up the left columns of the example text.
  297.    A negative value for this variable is incremented each time it is used.
  298.    @noindent uses this to inhibit indentation for a single paragraph.  */
  299. int inhibit_paragraph_indentation = 0;
  300.  
  301. /* Indentation that is pending insertion.  We have this for hacking lines
  302.    which look blank, but contain whitespace.  We want to treat those as
  303.    blank lines. */
  304. int pending_indent = 0;
  305.  
  306. /* The amount that indentation increases/decreases by. */
  307. int default_indentation_increment = DEFAULT_INDENTATION_INCREMENT;
  308.  
  309. /* Non-zero indicates that indentation is temporarily turned off. */
  310. int no_indent = 1;
  311.  
  312. /* Non-zero means forcing output text to be flushright. */
  313. int force_flush_right = 0;
  314.  
  315. /* Non-zero means that the footnote style for this document was set on
  316.    the command line, which overrides any other settings. */
  317. int footnote_style_preset = 0;
  318.  
  319. /* Non-zero means that we automatically number footnotes that have no
  320.    specified marker. */
  321. int number_footnotes = 1;
  322.  
  323. /* The current footnote number in this node.  Each time a new node is
  324.    started this is reset to 1. */
  325. int current_footnote_number = 1;
  326.  
  327. /* Command name in the process of being hacked. */
  328. char *command;
  329.  
  330. /* The index in our internal command table of the currently
  331.    executing command. */
  332. int command_index;
  333.  
  334. /* A stack of file information records.  If a new file is read in with
  335.    "@input", we remember the old input file state on this stack. */
  336. typedef struct fstack
  337. {
  338.   struct fstack *next;
  339.   char *filename;
  340.   char *text;
  341.   int size;
  342.   int offset;
  343.   int line_number;
  344. } FSTACK;
  345.  
  346. FSTACK *filestack = (FSTACK *) NULL;
  347.  
  348. /* Stuff for nodes. */
  349. /* The current nodes node name. */
  350. char *current_node = (char *)NULL;
  351.  
  352. /* The current nodes section level. */
  353. int current_section = 0;
  354.  
  355. /* The filename of the current input file.  This is never freed. */
  356. char *node_filename = (char *)NULL;
  357.  
  358. /* What we remember for each node. */
  359. typedef struct tentry
  360. {
  361.   struct tentry *next_ent;
  362.   char *node;        /* name of this node. */
  363.   char *prev;        /* name of "Prev:" for this node. */
  364.   char *next;        /* name of "Next:" for this node. */
  365.   char *up;        /* name of "Up:" for this node.   */
  366.   int position;        /* output file position of this node. */
  367.   int line_no;        /* defining line in source file. */
  368.   char *filename;    /* The file that this node was found in. */
  369.   int touched;        /* non-zero means this node has been referenced. */
  370.   int flags;        /* Room for growth.  Right now, contains 1 bit. */
  371. } TAG_ENTRY;
  372.  
  373. /* If node-a has a "Next" for node-b, but node-b has no "Prev" for node-a,
  374.    we turn on this flag bit in node-b's tag entry.  This means that when
  375.    it is time to validate node-b, we don't report an additional error
  376.    if there was no "Prev" field. */
  377. #define PREV_ERROR 0x1
  378. #define NEXT_ERROR 0x2
  379. #define UP_ERROR   0x4
  380. #define NO_WARN       0x8
  381. #define IS_TOP        0x10
  382.  
  383. TAG_ENTRY *tag_table = (TAG_ENTRY *) NULL;
  384.  
  385. #define HAVE_MACROS
  386. #if defined (HAVE_MACROS)
  387. /* Macro definitions for user-defined commands. */
  388. typedef struct {
  389.   char *name;            /* Name of the macro. */
  390.   char *definition;        /* Definition text. */
  391.   char *filename;        /* File where this macro is defined. */
  392.   int lineno;            /* Line number within FILENAME. */
  393. } MACRO_DEF;
  394.  
  395. void add_macro (), execute_macro ();
  396. MACRO_DEF *find_macro (), *delete_macro ();
  397. #endif /* HAVE_MACROS */
  398.  
  399. /* Menu reference, *note reference, and validation hacking. */
  400.  
  401. /* The various references that we know about. */
  402. enum reftype
  403. {
  404.   menu_reference, followed_reference
  405. };
  406.  
  407. /* A structure to remember references with.  A reference to a node is
  408.    either an entry in a menu, or a cross-reference made with [px]ref. */
  409. typedef struct node_ref
  410. {
  411.   struct node_ref *next;
  412.   char *node;            /* Name of node referred to. */
  413.   char *containing_node;    /* Name of node containing this reference. */
  414.   int line_no;            /* Line number where the reference occurs. */
  415.   int section;            /* Section level where the reference occurs. */
  416.   char *filename;        /* Name of file where the reference occurs. */
  417.   enum reftype type;        /* Type of reference, either menu or note. */
  418. } NODE_REF;
  419.  
  420. /* The linked list of such structures. */
  421. NODE_REF *node_references = (NODE_REF *) NULL;
  422.  
  423. /* Flag which tells us whether to examine menu lines or not. */
  424. int in_menu = 0;
  425.  
  426. /* Flags controlling the operation of the program. */
  427.  
  428. /* Default is to notify users of bad choices. */
  429. int print_warnings = 1;
  430.  
  431. /* Default is to check node references. */
  432. int validating = 1;
  433.  
  434. /* Non-zero means do not output "Node: Foo" for node separations. */
  435. int no_headers = 0;
  436.  
  437. /* Number of errors that we tolerate on a given fileset. */
  438. int max_error_level = 100;
  439.  
  440. /* Maximum number of references to a single node before complaining. */
  441. int reference_warning_limit = 1000;
  442.  
  443. /* Non-zero means print out information about what is going on when it
  444.    is going on. */
  445. int verbose_mode = 0;
  446.  
  447. /* The list of commands that we hack in texinfo.  Each one
  448.    has an associated function.  When the command is encountered in the
  449.    text, the associated function is called with START as the argument.
  450.    If the function expects arguments in braces, it remembers itself on
  451.    the stack.  When the corresponding close brace is encountered, the
  452.    function is called with END as the argument. */
  453.  
  454. #define START 0
  455. #define END 1
  456.  
  457. typedef struct brace_element
  458. {
  459.   struct brace_element *next;
  460.   FUNCTION *proc;
  461.   int pos, line;
  462. } BRACE_ELEMENT;
  463.  
  464. BRACE_ELEMENT *brace_stack = (BRACE_ELEMENT *) NULL;
  465.  
  466. /* Forward declarations. */
  467.  
  468. int insert_self (), cm_ignore_line ();
  469.  
  470. int
  471.   cm_tex (), cm_asterisk (), cm_dots (), cm_bullet (), cm_TeX (),
  472.   cm_copyright (), cm_code (), cm_samp (), cm_file (), cm_kbd (),
  473.   cm_key (), cm_ctrl (), cm_var (), cm_dfn (), cm_emph (), cm_strong (),
  474.   cm_cite (), cm_italic (), cm_bold (), cm_roman (), cm_title (), cm_w (),
  475.   cm_refill (), cm_titlefont ();
  476.  
  477. int
  478.   cm_chapter (), cm_unnumbered (), cm_appendix (), cm_top (),
  479.   cm_section (), cm_unnumberedsec (), cm_appendixsec (),
  480.   cm_subsection (), cm_unnumberedsubsec (), cm_appendixsubsec (),
  481.   cm_subsubsection (), cm_unnumberedsubsubsec (), cm_appendixsubsubsec (),
  482.   cm_heading (), cm_chapheading (), cm_subheading (), cm_subsubheading (),
  483.   cm_majorheading (), cm_raisesections (), cm_lowersections ();
  484.  
  485. /* All @defxxx commands map to cm_defun (). */
  486. int cm_defun ();
  487.  
  488. int
  489.   cm_node (), cm_menu (), cm_xref (), cm_ftable (), cm_vtable (), cm_pxref (),
  490.   cm_inforef (), cm_quotation (), cm_display (), cm_itemize (),
  491.   cm_enumerate (), cm_table (), cm_itemx (), cm_noindent (), cm_setfilename (),
  492.   cm_br (), cm_sp (), cm_page (), cm_group (), cm_center (), cm_include (),
  493.   cm_bye (), cm_item (), cm_end (), cm_infoinclude (), cm_ifinfo (),
  494.   cm_kindex (), cm_cindex (), cm_findex (), cm_pindex (), cm_vindex (),
  495.   cm_tindex (), cm_asis (), cm_synindex (), cm_printindex (), cm_minus (),
  496.   cm_footnote (), cm_force_abbreviated_whitespace (), cm_example (),
  497.   cm_smallexample (), cm_lisp (), cm_format (), cm_exdent (), cm_defindex (),
  498.   cm_defcodeindex (), cm_sc (), cm_result (), cm_expansion (), cm_equiv (),
  499.   cm_print (), cm_error (), cm_point (), cm_today (), cm_flushleft (),
  500.   cm_flushright (), cm_smalllisp (), cm_finalout (), cm_math (),
  501.   cm_cartouche (), cm_ignore_sentence_ender ();
  502.  
  503. /* Conditionals. */
  504. int cm_set (), cm_clear (), cm_ifset (), cm_ifclear (), cm_value ();
  505.  
  506. #if defined (HAVE_MACROS)
  507. /* Define a user-defined command which is simple substitution. */
  508. int cm_macro (), cm_unmacro ();
  509. #endif /* HAVE_MACROS */
  510.  
  511. /* Options. */
  512. int cm_paragraphindent (), cm_footnotestyle ();
  513.  
  514. /* Internals. */
  515. int do_nothing (), command_name_condition ();
  516. int misplaced_brace (), cm_obsolete ();
  517.  
  518. typedef struct
  519. {
  520.   char *name;
  521.   FUNCTION *proc;
  522.   int argument_in_braces;
  523. } COMMAND;
  524.  
  525. /* Stuff for defining commands on the fly. */
  526. COMMAND **user_command_array = (COMMAND **) NULL;
  527. int user_command_array_len = 0;
  528.  
  529. #define NO_BRACE_ARGS 0
  530. #define BRACE_ARGS 1
  531.  
  532. static COMMAND CommandTable[] = {
  533.   { "!", cm_ignore_sentence_ender, NO_BRACE_ARGS },
  534.   { "'", insert_self, NO_BRACE_ARGS },
  535.   { "*", cm_asterisk, NO_BRACE_ARGS },
  536.   { ".", cm_ignore_sentence_ender, NO_BRACE_ARGS },
  537.   { ":", cm_force_abbreviated_whitespace, NO_BRACE_ARGS },
  538.   { "?", cm_ignore_sentence_ender, NO_BRACE_ARGS },
  539.   { "|", do_nothing, NO_BRACE_ARGS },
  540.   { "@", insert_self, NO_BRACE_ARGS },
  541.   { " ", insert_self, NO_BRACE_ARGS },
  542.   { "\n", insert_self, NO_BRACE_ARGS },
  543.   { "TeX", cm_TeX, BRACE_ARGS },
  544.   { "`", insert_self, NO_BRACE_ARGS },
  545.   { "appendix", cm_appendix, NO_BRACE_ARGS },
  546.   { "appendixsection", cm_appendixsec, NO_BRACE_ARGS },
  547.   { "appendixsec", cm_appendixsec, NO_BRACE_ARGS },
  548.   { "appendixsubsec", cm_appendixsubsec, NO_BRACE_ARGS },
  549.   { "appendixsubsubsec", cm_appendixsubsubsec, NO_BRACE_ARGS },
  550.   { "asis", cm_asis, BRACE_ARGS },
  551.   { "b", cm_bold, BRACE_ARGS },
  552.   { "br", cm_br, NO_BRACE_ARGS },
  553.   { "bullet", cm_bullet, BRACE_ARGS },
  554.   { "bye", cm_bye, NO_BRACE_ARGS },
  555.   { "c", cm_ignore_line, NO_BRACE_ARGS },
  556.   { "cartouche", cm_cartouche, NO_BRACE_ARGS },
  557.   { "center", cm_center, NO_BRACE_ARGS },
  558.   { "chapheading", cm_chapheading, NO_BRACE_ARGS },
  559.   { "chapter", cm_chapter, NO_BRACE_ARGS },
  560.   { "cindex", cm_cindex, NO_BRACE_ARGS },
  561.   { "cite", cm_cite, BRACE_ARGS },
  562.   { "clear", cm_clear, NO_BRACE_ARGS },
  563.   { "code", cm_code, BRACE_ARGS },
  564.   { "comment", cm_ignore_line, NO_BRACE_ARGS },
  565.   { "contents", do_nothing, NO_BRACE_ARGS },
  566.   { "copyright", cm_copyright, BRACE_ARGS },
  567.   { "ctrl", cm_ctrl, BRACE_ARGS },
  568.   { "defcodeindex", cm_defcodeindex, NO_BRACE_ARGS },
  569.   { "defindex", cm_defindex, NO_BRACE_ARGS },
  570.   { "dfn", cm_dfn, BRACE_ARGS },
  571.  
  572. /* The `def' commands. */
  573.   { "deffn", cm_defun, NO_BRACE_ARGS },
  574.   { "deffnx", cm_defun, NO_BRACE_ARGS },
  575.   { "defun", cm_defun, NO_BRACE_ARGS },
  576.   { "defunx", cm_defun, NO_BRACE_ARGS },
  577.   { "defmac", cm_defun, NO_BRACE_ARGS },
  578.   { "defmacx", cm_defun, NO_BRACE_ARGS },
  579.   { "defspec", cm_defun, NO_BRACE_ARGS },
  580.   { "defspecx", cm_defun, NO_BRACE_ARGS },
  581.   { "defvr", cm_defun, NO_BRACE_ARGS },
  582.   { "defvrx", cm_defun, NO_BRACE_ARGS },
  583.   { "defvar", cm_defun, NO_BRACE_ARGS },
  584.   { "defvarx", cm_defun, NO_BRACE_ARGS },
  585.   { "defopt", cm_defun, NO_BRACE_ARGS },
  586.   { "defoptx", cm_defun, NO_BRACE_ARGS },
  587.   { "deftypefn", cm_defun, NO_BRACE_ARGS },
  588.   { "deftypefnx", cm_defun, NO_BRACE_ARGS },
  589.   { "deftypefun", cm_defun, NO_BRACE_ARGS },
  590.   { "deftypefunx", cm_defun, NO_BRACE_ARGS },
  591.   { "deftypevr", cm_defun, NO_BRACE_ARGS },
  592.   { "deftypevrx", cm_defun, NO_BRACE_ARGS },
  593.   { "deftypevar", cm_defun, NO_BRACE_ARGS },
  594.   { "deftypevarx", cm_defun, NO_BRACE_ARGS },
  595.   { "defcv", cm_defun, NO_BRACE_ARGS },
  596.   { "defcvx", cm_defun, NO_BRACE_ARGS },
  597.   { "defivar", cm_defun, NO_BRACE_ARGS },
  598.   { "defivarx", cm_defun, NO_BRACE_ARGS },
  599.   { "defop", cm_defun, NO_BRACE_ARGS },
  600.   { "defopx", cm_defun, NO_BRACE_ARGS },
  601.   { "defmethod", cm_defun, NO_BRACE_ARGS },
  602.   { "defmethodx", cm_defun, NO_BRACE_ARGS },
  603.   { "deftypemethod", cm_defun, NO_BRACE_ARGS },
  604.   { "deftypemethodx", cm_defun, NO_BRACE_ARGS },
  605.   { "deftp", cm_defun, NO_BRACE_ARGS },
  606.   { "deftpx", cm_defun, NO_BRACE_ARGS },
  607. /* The end of the `def' commands. */
  608.  
  609.   { "display", cm_display, NO_BRACE_ARGS },
  610.   { "dots", cm_dots, BRACE_ARGS },
  611.   { "dmn", do_nothing, BRACE_ARGS },
  612.   { "emph", cm_emph, BRACE_ARGS },
  613.   { "end", cm_end, NO_BRACE_ARGS },
  614.   { "enumerate", cm_enumerate, NO_BRACE_ARGS },
  615.   { "equiv", cm_equiv, BRACE_ARGS },
  616.   { "error", cm_error, BRACE_ARGS },
  617.   { "example", cm_example, NO_BRACE_ARGS },
  618.   { "exdent", cm_exdent, NO_BRACE_ARGS },
  619.   { "expansion", cm_expansion, BRACE_ARGS },
  620.   { "file", cm_file, BRACE_ARGS },
  621.   { "findex", cm_findex, NO_BRACE_ARGS },
  622.   { "finalout", do_nothing, NO_BRACE_ARGS },
  623.   { "flushleft", cm_flushleft, NO_BRACE_ARGS },
  624.   { "flushright", cm_flushright, NO_BRACE_ARGS },
  625.   { "format", cm_format, NO_BRACE_ARGS },
  626.   { "ftable", cm_ftable, NO_BRACE_ARGS },
  627.   { "group", cm_group, NO_BRACE_ARGS },
  628.   { "heading", cm_heading, NO_BRACE_ARGS },
  629.   { "headings", cm_ignore_line, NO_BRACE_ARGS },
  630.   { "i", cm_italic, BRACE_ARGS },
  631.   { "iappendix", cm_appendix, NO_BRACE_ARGS },
  632.   { "iappendixsection", cm_appendixsec, NO_BRACE_ARGS },
  633.   { "iappendixsec", cm_appendixsec, NO_BRACE_ARGS },
  634.   { "iappendixsubsec", cm_appendixsubsec, NO_BRACE_ARGS },
  635.   { "iappendixsubsubsec", cm_appendixsubsubsec, NO_BRACE_ARGS },
  636.   { "ichapter", cm_chapter, NO_BRACE_ARGS },
  637.   { "ifclear", cm_ifclear, NO_BRACE_ARGS },
  638.   { "ifinfo", cm_ifinfo, NO_BRACE_ARGS },
  639.   { "ifset", cm_ifset, NO_BRACE_ARGS },
  640.   { "iftex", command_name_condition, NO_BRACE_ARGS },
  641.   { "ignore", command_name_condition, NO_BRACE_ARGS },
  642.   { "include", cm_include, NO_BRACE_ARGS },
  643.   { "inforef", cm_inforef, BRACE_ARGS },
  644.   { "input", cm_include, NO_BRACE_ARGS },
  645.   { "isection", cm_section, NO_BRACE_ARGS },
  646.   { "isubsection", cm_subsection, NO_BRACE_ARGS },
  647.   { "isubsubsection", cm_subsubsection, NO_BRACE_ARGS },
  648.   { "item", cm_item, NO_BRACE_ARGS },
  649.   { "itemize", cm_itemize, NO_BRACE_ARGS },
  650.   { "itemx", cm_itemx, NO_BRACE_ARGS },
  651.   { "iunnumbered", cm_unnumbered, NO_BRACE_ARGS },
  652.   { "iunnumberedsec", cm_unnumberedsec, NO_BRACE_ARGS },
  653.   { "iunnumberedsubsec", cm_unnumberedsubsec, NO_BRACE_ARGS },
  654.   { "iunnumberedsubsubsec", cm_unnumberedsubsubsec, NO_BRACE_ARGS },
  655.   { "kbd", cm_kbd, BRACE_ARGS },
  656.   { "key", cm_key, BRACE_ARGS },
  657.   { "kindex", cm_kindex, NO_BRACE_ARGS },
  658.   { "lowersections", cm_lowersections, NO_BRACE_ARGS },
  659.   { "lisp", cm_lisp, NO_BRACE_ARGS },
  660.   { "macro", cm_macro, NO_BRACE_ARGS },
  661.   { "majorheading", cm_majorheading, NO_BRACE_ARGS },
  662.   { "math", cm_math, BRACE_ARGS },
  663.   { "menu", cm_menu, NO_BRACE_ARGS },
  664.   { "minus", cm_minus, BRACE_ARGS },
  665.   { "need", cm_ignore_line, NO_BRACE_ARGS },
  666.   { "node", cm_node, NO_BRACE_ARGS },
  667.   { "noindent", cm_noindent, NO_BRACE_ARGS },
  668.   { "nwnode", cm_node, NO_BRACE_ARGS },
  669.   { "overfullrule", cm_ignore_line, NO_BRACE_ARGS },
  670.   { "page", do_nothing, NO_BRACE_ARGS },
  671.   { "pindex", cm_pindex, NO_BRACE_ARGS },
  672.   { "point", cm_point, BRACE_ARGS },
  673.   { "print", cm_print, BRACE_ARGS },
  674.   { "printindex", cm_printindex, NO_BRACE_ARGS },
  675.   { "pxref", cm_pxref, BRACE_ARGS },
  676.   { "quotation", cm_quotation, NO_BRACE_ARGS },
  677.   { "r", cm_roman, BRACE_ARGS },
  678.   { "raisesections", cm_raisesections, NO_BRACE_ARGS },
  679.   { "ref", cm_xref, BRACE_ARGS },
  680.   { "refill", cm_refill, NO_BRACE_ARGS },
  681.   { "result", cm_result, BRACE_ARGS },
  682.   { "samp", cm_samp, BRACE_ARGS },
  683.   { "sc", cm_sc, BRACE_ARGS },
  684.   { "section", cm_section, NO_BRACE_ARGS },
  685.   { "set", cm_set, NO_BRACE_ARGS },
  686.   { "setchapternewpage", cm_ignore_line, NO_BRACE_ARGS },
  687.   { "setchapterstyle", cm_ignore_line, NO_BRACE_ARGS },
  688.   { "setfilename", cm_setfilename, NO_BRACE_ARGS },
  689.   { "settitle", cm_ignore_line, NO_BRACE_ARGS },
  690.   { "shortcontents", do_nothing, NO_BRACE_ARGS },
  691.   { "shorttitlepage", command_name_condition, NO_BRACE_ARGS },
  692.   { "smallbook", cm_ignore_line, NO_BRACE_ARGS },
  693.   { "smallexample", cm_smallexample, NO_BRACE_ARGS },
  694.   { "smalllisp", cm_smalllisp, NO_BRACE_ARGS },
  695.   { "sp", cm_sp, NO_BRACE_ARGS },
  696.   { "strong", cm_strong, BRACE_ARGS },
  697.   { "subheading", cm_subheading, NO_BRACE_ARGS },
  698.   { "subsection", cm_subsection, NO_BRACE_ARGS },
  699.   { "subsubheading", cm_subsubheading, NO_BRACE_ARGS },
  700.   { "subsubsection", cm_subsubsection, NO_BRACE_ARGS },
  701.   { "summarycontents", do_nothing, NO_BRACE_ARGS },
  702.   { "syncodeindex", cm_synindex, NO_BRACE_ARGS },
  703.   { "synindex", cm_synindex, NO_BRACE_ARGS },
  704.   { "t", cm_title, BRACE_ARGS },
  705.   { "table", cm_table, NO_BRACE_ARGS },
  706.   { "tex", command_name_condition, NO_BRACE_ARGS },
  707.   { "tindex", cm_tindex, NO_BRACE_ARGS },
  708.   { "titlefont", cm_titlefont, BRACE_ARGS },
  709.   { "titlepage", command_name_condition, NO_BRACE_ARGS },
  710.   { "titlespec", command_name_condition, NO_BRACE_ARGS },
  711.   { "today", cm_today, BRACE_ARGS },
  712.   { "top", cm_top, NO_BRACE_ARGS  },
  713.   { "unmacro", cm_unmacro, NO_BRACE_ARGS },
  714.   { "unnumbered", cm_unnumbered, NO_BRACE_ARGS },
  715.   { "unnumberedsec", cm_unnumberedsec, NO_BRACE_ARGS },
  716.   { "unnumberedsubsec", cm_unnumberedsubsec, NO_BRACE_ARGS },
  717.   { "unnumberedsubsubsec", cm_unnumberedsubsubsec, NO_BRACE_ARGS },
  718.   { "value", cm_value, BRACE_ARGS },
  719.   { "var", cm_var, BRACE_ARGS },
  720.   { "vindex", cm_vindex, NO_BRACE_ARGS },
  721.   { "vtable", cm_vtable, NO_BRACE_ARGS },
  722.   { "w", cm_w, BRACE_ARGS },
  723.   { "xref", cm_xref, BRACE_ARGS },
  724.   { "{", insert_self, NO_BRACE_ARGS },
  725.   { "}", insert_self, NO_BRACE_ARGS },
  726.  
  727.   /* Some obsoleted commands. */
  728.   { "infotop", cm_obsolete, NO_BRACE_ARGS },
  729.   { "infounnumbered", cm_obsolete, NO_BRACE_ARGS },
  730.   { "infounnumberedsec", cm_obsolete, NO_BRACE_ARGS },
  731.   { "infounnumberedsubsec", cm_obsolete, NO_BRACE_ARGS },
  732.   { "infounnumberedsubsubsec", cm_obsolete, NO_BRACE_ARGS },
  733.   { "infoappendix", cm_obsolete, NO_BRACE_ARGS },
  734.   { "infoappendixsec", cm_obsolete, NO_BRACE_ARGS },
  735.   { "infoappendixsubsec", cm_obsolete, NO_BRACE_ARGS },
  736.   { "infoappendixsubsubsec", cm_obsolete, NO_BRACE_ARGS },
  737.   { "infochapter", cm_obsolete, NO_BRACE_ARGS },
  738.   { "infosection", cm_obsolete, NO_BRACE_ARGS },
  739.   { "infosubsection", cm_obsolete, NO_BRACE_ARGS },
  740.   { "infosubsubsection", cm_obsolete, NO_BRACE_ARGS },
  741.  
  742.   /* Now @include does what this was supposed to. */
  743.   { "infoinclude", cm_infoinclude, NO_BRACE_ARGS },
  744.   { "footnote", cm_footnote, NO_BRACE_ARGS}, /* self-arg eater */
  745.   { "footnotestyle", cm_footnotestyle, NO_BRACE_ARGS },
  746.   { "paragraphindent", cm_paragraphindent, NO_BRACE_ARGS },
  747.  
  748.   {(char *) NULL, (FUNCTION *) NULL}, NO_BRACE_ARGS};
  749.  
  750. int major_version = 1;
  751. int minor_version = 55;
  752.  
  753. struct option long_options[] =
  754. {
  755.   { "error-limit", 1, 0, 'e' },            /* formerly -el */
  756.   { "fill-column", 1, 0, 'f' },            /* formerly -fc */
  757.   { "footnote-style", 1, 0, 's' },        /* formerly -ft */
  758.   { "no-headers", 0, &no_headers, 1 },        /* Do not output Node: foo */
  759.   { "no-pointer-validate", 0, &validating, 0 }, /* formerly -nv */
  760.   { "no-validate", 0, &validating, 0 },        /* formerly -nv */
  761.   { "no-split", 0, &splitting, 0 },        /* formerly -ns */
  762.   { "no-warn", 0, &print_warnings, 0 },        /* formerly -nw */
  763.   { "number-footnotes", 0, &number_footnotes, 1 },
  764.   { "no-number-footnotes", 0, &number_footnotes, 0 },
  765.   { "output", 1, 0, 'o' },
  766.   { "paragraph-indent", 1, 0, 'p' },        /* formerly -pi */
  767.   { "reference-limit", 1, 0, 'r' },        /* formerly -rl */
  768.   { "verbose", 0, &verbose_mode, 1 },        /* formerly -verbose */
  769.   { "version", 0, 0, 'V' },
  770.   {NULL, 0, NULL, 0}
  771. };
  772.  
  773. /* Values for calling handle_variable_internal (). */
  774. #define SET    1
  775. #define CLEAR    2
  776. #define IFSET    3
  777. #define IFCLEAR    4
  778.  
  779. /* **************************************************************** */
  780. /*                                    */
  781. /*            Main ()  Start of code              */
  782. /*                                        */
  783. /* **************************************************************** */
  784.  
  785. /* For each file mentioned in the command line, process it, turning
  786.    texinfo commands into wonderfully formatted output text. */
  787. main (argc, argv)
  788.      int argc;
  789.      char **argv;
  790. {
  791.   extern int errors_printed;
  792.   char *filename_part ();
  793.   int c, ind;
  794.  
  795.   /* The name of this program is the last filename in argv[0]. */
  796.   progname = filename_part (argv[0]);
  797.  
  798.   /* Parse argument flags from the input line. */
  799.   while ((c = getopt_long
  800.       (argc, argv, "D:U:I:f:o:p:e:r:s:V", long_options, &ind))
  801.      != EOF)
  802.     {
  803.       if (c == 0 && long_options[ind].flag == 0)
  804.     c = long_options[ind].val;
  805.  
  806.       switch (c)
  807.     {
  808.       /* User specified variable to set or clear? */
  809.     case 'D':
  810.     case 'U':
  811.       handle_variable_internal ((c == 'D') ? SET : CLEAR, optarg);
  812.       break;
  813.  
  814.       /* User specified include file path? */
  815.     case 'I':
  816.       if (!include_files_path)
  817.         include_files_path = savestring (".");
  818.  
  819.       include_files_path = (char *)
  820.         xrealloc (include_files_path,
  821.               2 + strlen (include_files_path) + strlen (optarg));
  822.       strcat (include_files_path, ":");
  823.       strcat (include_files_path, optarg);
  824.       break;
  825.  
  826.       /* User specified fill_column? */
  827.     case 'f':
  828.       if (sscanf (optarg, "%d", &fill_column) != 1)
  829.         usage ();
  830.       break;
  831.  
  832.       /* User specified output file? */
  833.     case 'o':
  834.       command_output_filename = savestring (optarg);
  835.       break;
  836.  
  837.       /* User specified paragraph indent (paragraph_start_index)? */
  838.     case 'p':
  839.       if (set_paragraph_indent (optarg) < 0)
  840.         usage ();
  841.       break;
  842.  
  843.       /* User specified error level? */
  844.     case 'e':
  845.       if (sscanf (optarg, "%d", &max_error_level) != 1)
  846.         usage ();
  847.       break;
  848.  
  849.       /* User specified reference warning limit? */
  850.     case 'r':
  851.       if (sscanf (optarg, "%d", &reference_warning_limit) != 1)
  852.         usage ();
  853.       break;
  854.  
  855.       /* User specified footnote style? */
  856.     case 's':
  857.       if (set_footnote_style (optarg) < 0)
  858.         usage ();
  859.       footnote_style_preset = 1;
  860.       break;
  861.  
  862.       /* User requested version info? */
  863.     case 'V':
  864.       print_version_info ();
  865.       exit (NO_ERROR);
  866.       break;
  867.  
  868.     case '?':
  869.       usage ();
  870.     }
  871.     }
  872.  
  873.   if (optind == argc)
  874.     usage ();
  875.   else if (verbose_mode)
  876.     print_version_info ();
  877.  
  878.   /* Remaining arguments are file names of texinfo files.
  879.      Convert them, one by one. */
  880.   while (optind != argc)
  881.     convert (argv[optind++]);
  882.  
  883.   if (errors_printed)
  884.     exit (SYNTAX);
  885.   else
  886.     exit (NO_ERROR);
  887. }
  888.  
  889. /* Display the version info of this invocation of Makeinfo. */
  890. print_version_info ()
  891. {
  892.   fprintf (stderr, "This is GNU Makeinfo version %d.%d.\n",
  893.        major_version, minor_version);
  894. }
  895.  
  896. /* **************************************************************** */
  897. /*                                    */
  898. /*            Generic Utilities                */
  899. /*                                    */
  900. /* **************************************************************** */
  901.  
  902. /* Just like malloc, but kills the program in case of fatal error. */
  903. void *
  904. xmalloc (nbytes)
  905.      int nbytes;
  906. {
  907.   void *temp = (void *) malloc (nbytes);
  908.  
  909.   if (nbytes && temp == (void *)NULL)
  910.     memory_error ("xmalloc", nbytes);
  911.  
  912.   return (temp);
  913. }
  914.  
  915. /* Like realloc (), but barfs if there isn't enough memory. */
  916. void *
  917. xrealloc (pointer, nbytes)
  918.      void *pointer;
  919.      int nbytes;
  920. {
  921.   void *temp;
  922.  
  923.   if (!pointer)
  924.     temp = (void *)xmalloc (nbytes);
  925.   else
  926.     temp = (void *)realloc (pointer, nbytes);
  927.  
  928.   if (nbytes && !temp)
  929.     memory_error ("xrealloc", nbytes);
  930.  
  931.   return (temp);
  932. }
  933.  
  934. memory_error (callers_name, bytes_wanted)
  935.      char *callers_name;
  936.      int bytes_wanted;
  937. {
  938.   char printable_string[80];
  939.  
  940.   sprintf (printable_string,
  941.        "Virtual memory exhausted in %s ()!  Needed %d bytes.",
  942.        callers_name, bytes_wanted);
  943.  
  944.   error (printable_string);
  945.   abort ();
  946. }
  947.  
  948. /* Tell the user how to use this program. */
  949. usage ()
  950. {
  951.   fprintf (stderr, "Usage: %s [options] texinfo-file...\n\
  952. \n\
  953. This program accepts as input files of texinfo commands and text\n\
  954. and outputs a file suitable for reading with GNU Info.\n\
  955. \n\
  956. The options are:\n\
  957. `-I DIR'              to add DIR to the directory search list for including\n\
  958.                       files with the `@include' command.\n\
  959. -D VAR                to define a variable, as with `@set'.\n\
  960. -U VAR                to undefine a variable, as with `@clear'.\n\
  961. `--no-validate'       to suppress node cross reference validation.\n\
  962. `--no-warn'           to suppress warning messages (errors are still output).\n\
  963. `--no-split'          to suppress the splitting of large files.\n\
  964. `--no-headers'        to suppress the output of Node: Foo headers.\n\
  965. `--verbose'           to print information about what is being done.\n\
  966. `--version'           to print the version number of Makeinfo.\n\
  967. `--output FILE' or `-o FILE'\n\
  968.                       to specify the output file.  When you specify the\n\
  969.                       output file in this way, any `@setfilename' in the\n\
  970.                       input file is ignored.\n\
  971. `--paragraph-indent NUM'\n\
  972.                       to set the paragraph indent to NUM (default %d).\n\
  973. `--fill-column NUM'   to set the filling column to NUM (default %d).\n\
  974. `--error-limit NUM'   to set the error limit to NUM (default %d).\n\
  975. `--reference-limit NUM'\n\
  976.                       to set the reference warning limit to NUM (default %d).\n\
  977. `--footnote-style STYLE'\n\
  978.                       to set the footnote style to STYLE.  STYLE should\n\
  979.                       either be `separate' to place footnotes in their own\n\
  980.                       node, or `end', to place the footnotes at the end of\n\
  981.                       the node in which they are defined (the default).\n\n",
  982.        progname, paragraph_start_indent,
  983.        fill_column, max_error_level, reference_warning_limit);
  984.   exit (FATAL);
  985. }
  986.  
  987. /* **************************************************************** */
  988. /*                                    */
  989. /*            Manipulating Lists                  */
  990. /*                                        */
  991. /* **************************************************************** */
  992.  
  993. typedef struct generic_list {
  994.   struct generic_list *next;
  995. } GENERIC_LIST;
  996.  
  997. /* Reverse the chain of structures in LIST.  Output the new head
  998.    of the chain.  You should always assign the output value of this
  999.    function to something, or you will lose the chain. */
  1000. GENERIC_LIST *
  1001. reverse_list (list)
  1002.      register GENERIC_LIST *list;
  1003. {
  1004.   register GENERIC_LIST *next;
  1005.   register GENERIC_LIST *prev = (GENERIC_LIST *) NULL;
  1006.  
  1007.   while (list)
  1008.     {
  1009.       next = list->next;
  1010.       list->next = prev;
  1011.       prev = list;
  1012.       list = next;
  1013.     }
  1014.   return (prev);
  1015. }
  1016.  
  1017.  
  1018. /* **************************************************************** */
  1019. /*                                    */
  1020. /*            Pushing and Popping Files               */
  1021. /*                                    */
  1022. /* **************************************************************** */
  1023.  
  1024. /* Find and load the file named FILENAME.  Return a pointer to
  1025.    the loaded file, or NULL if it can't be loaded. */
  1026. char *
  1027. find_and_load (filename)
  1028.      char *filename;
  1029. {
  1030.   struct stat fileinfo;
  1031.   int file = -1, n, i, count = 0;
  1032.   char *fullpath, *result, *get_file_info_in_path ();
  1033.  
  1034.   result = fullpath = (char *)NULL;
  1035.  
  1036.   fullpath = get_file_info_in_path (filename, include_files_path, &fileinfo);
  1037.  
  1038.   if (!fullpath)
  1039.     goto error_exit;
  1040.  
  1041.   filename = fullpath;
  1042.  
  1043.   file = open (filename, O_RDONLY);
  1044.   if (file < 0)
  1045.     goto error_exit;
  1046.  
  1047.   /* Load the file. */
  1048.   result = (char *)xmalloc (1 + fileinfo.st_size);
  1049.  
  1050.   /* VMS stat lies about the st_size value.  The actual number of
  1051.      readable bytes is always less than this value.  The arcane
  1052.      mysteries of VMS/RMS are too much to probe, so this hack
  1053.     suffices to make things work. */
  1054. #if defined (VMS)
  1055.   while ((n = read (file, result + count, fileinfo.st_size)) > 0)
  1056.     count += n;
  1057.   if (n == -1)
  1058. #else /* !VMS */
  1059.     count = fileinfo.st_size;
  1060.     if (read (file, result, fileinfo.st_size) != fileinfo.st_size)
  1061. #endif /* !VMS */
  1062.   error_exit:
  1063.     {
  1064.       if (result)
  1065.     free (result);
  1066.  
  1067.       if (fullpath)
  1068.     free (fullpath);
  1069.  
  1070.       if (file != -1)
  1071.     close (file);
  1072.  
  1073.       return ((char *) NULL);
  1074.     }
  1075.   close (file);
  1076.  
  1077.   /* Set the globals to the new file. */
  1078.   input_text = result;
  1079.   size_of_input_text = count;
  1080.   input_filename = savestring (fullpath);
  1081.   node_filename = savestring (fullpath);
  1082.   input_text_offset = 0;
  1083.   line_number = 1;
  1084.   /* Not strictly necessary.  This magic prevents read_token () from doing
  1085.      extra unnecessary work each time it is called (that is a lot of times).
  1086.      The SIZE_OF_INPUT_TEXT is one past the actual end of the text. */
  1087.   input_text[size_of_input_text] = '\n';
  1088.   return (result);
  1089. }
  1090.  
  1091. /* Save the state of the current input file. */
  1092. pushfile ()
  1093. {
  1094.   FSTACK *newstack = (FSTACK *) xmalloc (sizeof (FSTACK));
  1095.   newstack->filename = input_filename;
  1096.   newstack->text = input_text;
  1097.   newstack->size = size_of_input_text;
  1098.   newstack->offset = input_text_offset;
  1099.   newstack->line_number = line_number;
  1100.   newstack->next = filestack;
  1101.  
  1102.   filestack = newstack;
  1103.   push_node_filename ();
  1104. }
  1105.  
  1106. /* Make the current file globals be what is on top of the file stack. */
  1107. popfile ()
  1108. {
  1109.   extern int executing_string;
  1110.   FSTACK *temp = filestack;
  1111.  
  1112.   if (!filestack)
  1113.     abort ();            /* My fault.  I wonder what I did? */
  1114.  
  1115.   /* Make sure that commands with braces have been satisfied. */
  1116.   if (!executing_string)
  1117.     discard_braces ();
  1118.  
  1119.   /* Get the top of the stack into the globals. */
  1120.   input_filename = filestack->filename;
  1121.   input_text = filestack->text;
  1122.   size_of_input_text = filestack->size;
  1123.   input_text_offset = filestack->offset;
  1124.   line_number = filestack->line_number;
  1125.  
  1126.   /* Pop the stack. */
  1127.   filestack = filestack->next;
  1128.   free (temp);
  1129.   pop_node_filename ();
  1130. }
  1131.  
  1132. /* Flush all open files on the file stack. */
  1133. flush_file_stack ()
  1134. {
  1135.   while (filestack)
  1136.     {
  1137.       free (input_filename);
  1138.       free (input_text);
  1139.       popfile ();
  1140.     }
  1141. }
  1142.  
  1143. int node_filename_stack_index = 0;
  1144. int node_filename_stack_size = 0;
  1145. char **node_filename_stack = (char **)NULL;
  1146.  
  1147. push_node_filename ()
  1148. {
  1149.   if (node_filename_stack_index + 1 > node_filename_stack_size)
  1150.     {
  1151.       if (!node_filename_stack)
  1152.     node_filename_stack =
  1153.       (char **)xmalloc ((node_filename_stack_size += 10)
  1154.                 * sizeof (char *));
  1155.       else
  1156.     node_filename_stack =
  1157.       (char **)xrealloc (node_filename_stack,
  1158.                  (node_filename_stack_size + 10)
  1159.                  * sizeof (char *));
  1160.     }
  1161.  
  1162.   node_filename_stack[node_filename_stack_index] = node_filename;
  1163.   node_filename_stack_index++;
  1164. }
  1165.  
  1166. pop_node_filename ()
  1167. {
  1168.   node_filename = node_filename_stack[--node_filename_stack_index];
  1169. }
  1170.  
  1171. /* Return just the simple part of the filename; i.e. the
  1172.    filename without the path information, or extensions.
  1173.    This conses up a new string. */
  1174. char *
  1175. filename_part (filename)
  1176.      char *filename;
  1177. {
  1178.   char *basename;
  1179.  
  1180.   basename = strrchr (filename, '/');
  1181.   if (!basename)
  1182.     basename = filename;
  1183.   else
  1184.     basename++;
  1185.  
  1186.   basename = savestring (basename);
  1187. #if defined (REMOVE_OUTPUT_EXTENSIONS)
  1188.  
  1189.   /* See if there is an extension to remove.  If so, remove it. */
  1190.   {
  1191.     char *temp;
  1192.  
  1193.     temp = strrchr (basename, '.');
  1194.     if (temp)
  1195.       *temp = '\0';
  1196.   }
  1197. #endif /* REMOVE_OUTPUT_EXTENSIONS */
  1198.   return (basename);
  1199. }
  1200.  
  1201. /* Return the pathname part of filename.  This can be NULL. */
  1202. char *
  1203. pathname_part (filename)
  1204.      char *filename;
  1205. {
  1206.   char *expand_filename ();
  1207.   char *result = (char *) NULL;
  1208.   register int i;
  1209.  
  1210.   filename = expand_filename (filename, "");
  1211.  
  1212.   i = strlen (filename) - 1;
  1213.  
  1214.   while (i && filename[i] != '/')
  1215.     i--;
  1216.   if (filename[i] == '/')
  1217.     i++;
  1218.  
  1219.   if (i)
  1220.     {
  1221.       result = (char *)xmalloc (1 + i);
  1222.       strncpy (result, filename, i);
  1223.       result[i] = '\0';
  1224.     }
  1225.   free (filename);
  1226.   return (result);
  1227. }
  1228.  
  1229. /* Return the expansion of FILENAME. */
  1230. char *
  1231. expand_filename (filename, input_name)
  1232.      char *filename, *input_name;
  1233. {
  1234.   char *full_pathname ();
  1235.   filename = full_pathname (filename);
  1236.  
  1237.   if (filename[0] == '.')
  1238.     return (filename);
  1239.  
  1240.   if (filename[0] != '/' && input_name[0] == '/')
  1241.     {
  1242.       /* Make it so that relative names work. */
  1243.       char *result;
  1244.       int i = strlen (input_name) - 1;
  1245.  
  1246.       result = (char *)xmalloc (1 + strlen (input_name) + strlen (filename));
  1247.       strcpy (result, input_name);
  1248.  
  1249.       while (result[i] != '/' && i)
  1250.     i--;
  1251.  
  1252.       if (result[i] == '/')
  1253.     i++;
  1254.  
  1255.       strcpy (&result[i], filename);
  1256.       free (filename);
  1257.       return (result);
  1258.     }
  1259.   return (filename);
  1260. }
  1261.  
  1262. /* Return the full path to FILENAME. */
  1263. char *
  1264. full_pathname (filename)
  1265.      char *filename;
  1266. {
  1267.   int initial_character;
  1268.  
  1269.   if (filename && (initial_character = *filename))
  1270.     {
  1271.       if (initial_character == '/')
  1272.     return (savestring (filename));
  1273.       if (initial_character != '~')
  1274.     {
  1275.       return (savestring (filename));
  1276.     }
  1277.       else
  1278.     {
  1279.       if (filename[1] == '/')
  1280.         {
  1281.           /* Return the concatenation of HOME and the rest of the string. */
  1282.           char *temp_home;
  1283.           char *temp_name;
  1284.  
  1285.           temp_home = (char *) getenv ("HOME");
  1286.           temp_name = (char *)xmalloc (strlen (&filename[2])
  1287.                        + 1
  1288.                        + temp_home ? strlen (temp_home)
  1289.                        : 0);
  1290.           if (temp_home)
  1291.         strcpy (temp_name, temp_home);
  1292.  
  1293.           strcat (temp_name, &filename[2]);
  1294.           return (temp_name);
  1295.         }
  1296.       else
  1297.         {
  1298.           struct passwd *user_entry;
  1299.           int i, c;
  1300.           char *username = (char *)xmalloc (257);
  1301.           char *temp_name;
  1302.  
  1303.           for (i = 1; c = filename[i]; i++)
  1304.         {
  1305.           if (c == '/')
  1306.             break;
  1307.           else
  1308.             username[i - 1] = c;
  1309.         }
  1310.           if (c)
  1311.         username[i - 1] = '\0';
  1312.  
  1313.           user_entry = getpwnam (username);
  1314.  
  1315.           if (!user_entry)
  1316.         return (savestring (filename));
  1317.  
  1318.           temp_name = (char *)xmalloc (1 + strlen (user_entry->pw_dir)
  1319.                        + strlen (&filename[i]));
  1320.           strcpy (temp_name, user_entry->pw_dir);
  1321.           strcat (temp_name, &filename[i]);
  1322.           return (temp_name);
  1323.         }
  1324.     }
  1325.     }
  1326.   else
  1327.     {
  1328.       return (savestring (filename));
  1329.     }
  1330. }
  1331.  
  1332. /* **************************************************************** */
  1333. /*                                    */
  1334. /*            Error Handling                    */
  1335. /*                                    */
  1336. /* **************************************************************** */
  1337.  
  1338. /* Number of errors encountered. */
  1339. int errors_printed = 0;
  1340.  
  1341. /* Print the last error gotten from the file system. */
  1342. fs_error (filename)
  1343.      char *filename;
  1344. {
  1345.   remember_error ();
  1346.   perror (filename);
  1347.   return (0);
  1348. }
  1349.  
  1350. /* Print an error message, and return false. */
  1351. #if defined (HAVE_VARARGS_H) && defined (HAVE_VFPRINTF)
  1352.  
  1353. int
  1354. error (va_alist)
  1355.      va_dcl
  1356. {
  1357.   char *format;
  1358.   va_list args;
  1359.  
  1360.   remember_error ();
  1361.   va_start (args);
  1362.   format = va_arg (args, char *);
  1363.   vfprintf (stderr, format, args);
  1364.   va_end (args);
  1365.   fprintf (stderr, "\n");
  1366. }
  1367.  
  1368. /* Just like error (), but print the line number as well. */
  1369. int
  1370. line_error (va_alist)
  1371.      va_dcl
  1372. {
  1373.   char *format;
  1374.   va_list args;
  1375.  
  1376.   remember_error ();
  1377.   va_start (args);
  1378.   format = va_arg (args, char *);
  1379.   fprintf (stderr, "%s:%d: ", input_filename, line_number);
  1380.   vfprintf (stderr, format, args);
  1381.   fprintf (stderr, ".\n");
  1382.   va_end (args);
  1383.   return ((int) 0);
  1384. }
  1385.  
  1386. int
  1387. warning (va_alist)
  1388.      va_dcl
  1389. {
  1390.   char *format;
  1391.   va_list args;
  1392.  
  1393.   va_start (args);
  1394.   format = va_arg (args, char *);
  1395.   if (print_warnings)
  1396.     {
  1397.       fprintf (stderr, "%s:%d: Warning: ", input_filename, line_number);
  1398.       vfprintf (stderr, format, args);
  1399.       fprintf (stderr, ".\n");
  1400.     }
  1401.   va_end (args);
  1402.   return ((int) 0);
  1403. }
  1404.  
  1405. #else /* !(HAVE_VARARGS_H && HAVE_VFPRINTF) */
  1406.  
  1407. int
  1408. error (format, arg1, arg2, arg3, arg4, arg5)
  1409.      char *format;
  1410. {
  1411.   remember_error ();
  1412.   fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
  1413.   fprintf (stderr, "\n");
  1414.   return ((int) 0);
  1415. }
  1416.  
  1417. /* Just like error (), but print the line number as well. */
  1418. int
  1419. line_error (format, arg1, arg2, arg3, arg4, arg5)
  1420.      char *format;
  1421. {
  1422.   remember_error ();
  1423.   fprintf (stderr, "%s:%d: ", input_filename, line_number);
  1424.   fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
  1425.   fprintf (stderr, ".\n");
  1426.   return ((int) 0);
  1427. }
  1428.  
  1429. int
  1430. warning (format, arg1, arg2, arg3, arg4, arg5)
  1431.      char *format;
  1432. {
  1433.   if (print_warnings)
  1434.     {
  1435.       fprintf (stderr, "%s:%d: Warning: ", input_filename, line_number);
  1436.       fprintf (stderr, format, arg1, arg2, arg3, arg4, arg5);
  1437.       fprintf (stderr, ".\n");
  1438.     }
  1439.   return ((int) 0);
  1440. }
  1441.  
  1442. #endif /* !(HAVE_VARARGS_H && HAVE_VFPRINTF) */
  1443.  
  1444. /* Remember that an error has been printed.  If this is the first
  1445.    error printed, then tell them which program is printing them.
  1446.    If more than max_error_level have been printed, then exit the
  1447.    program. */
  1448. remember_error ()
  1449. {
  1450.   errors_printed++;
  1451.   if (max_error_level && (errors_printed > max_error_level))
  1452.     {
  1453.       fprintf (stderr, "Too many errors!  Gave up.\n");
  1454.       flush_file_stack ();
  1455.       cm_bye ();
  1456.       exit (1);
  1457.     }
  1458. }
  1459.  
  1460. /* **************************************************************** */
  1461. /*                                    */
  1462. /*            Hacking Tokens and Strings            */
  1463. /*                                    */
  1464. /* **************************************************************** */
  1465.  
  1466. /* Return the next token as a string pointer.  We cons the
  1467.    string. */
  1468. char *
  1469. read_token ()
  1470. {
  1471.   int i, character;
  1472.   char *result;
  1473.  
  1474.   /* If the first character to be read is self-delimiting, then that
  1475.      is the command itself. */
  1476.   character = curchar ();
  1477.   if (self_delimiting (character))
  1478.     {
  1479.       input_text_offset++;
  1480.       result = savestring (" ");
  1481.       *result = character;
  1482.       return (result);
  1483.     }
  1484.  
  1485.   for (i = 0; ((input_text_offset != size_of_input_text)
  1486.            && (character = curchar ())
  1487.            && command_char (character));
  1488.        i++, input_text_offset++);
  1489.   result = (char *)xmalloc (i + 1);
  1490.   memcpy (result, &input_text[input_text_offset - i], i);
  1491.   result[i] = '\0';
  1492.   return (result);
  1493. }
  1494.  
  1495. /* Return non-zero if CHARACTER is self-delimiting. */
  1496. int
  1497. self_delimiting (character)
  1498.      int character;
  1499. {
  1500.   return (member (character, "{}:.@*'`,!?; \n"));
  1501. }
  1502.  
  1503. /* Clear whitespace from the front and end of string. */
  1504. canon_white (string)
  1505.      char *string;
  1506. {
  1507.   int len = strlen (string);
  1508.   int x;
  1509.  
  1510.   if (!len)
  1511.     return;
  1512.  
  1513.   for (x = 0; x < len; x++)
  1514.     {
  1515.       if (!cr_or_whitespace (string[x]))
  1516.     {
  1517.       strcpy (string, string + x);
  1518.       break;
  1519.     }
  1520.     }
  1521.   len = strlen (string);
  1522.   if (len)
  1523.     len--;
  1524.   while (len > -1 && cr_or_whitespace (string[len]))
  1525.     len--;
  1526.   string[len + 1] = '\0';
  1527. }
  1528.  
  1529. /* Bash STRING, replacing all whitespace with just one space. */
  1530. fix_whitespace (string)
  1531.      char *string;
  1532. {
  1533.   char *temp = (char *)xmalloc (strlen (string) + 1);
  1534.   int string_index = 0;
  1535.   int temp_index = 0;
  1536.   int c;
  1537.  
  1538.   canon_white (string);
  1539.  
  1540.   while (string[string_index])
  1541.     {
  1542.       c = temp[temp_index++] = string[string_index++];
  1543.  
  1544.       if (c == ' ' || c == '\n' || c == '\t')
  1545.     {
  1546.       temp[temp_index - 1] = ' ';
  1547.       while ((c = string[string_index]) && (c == ' ' ||
  1548.                         c == '\t' ||
  1549.                         c == '\n'))
  1550.         string_index++;
  1551.     }
  1552.     }
  1553.   temp[temp_index] = '\0';
  1554.   strcpy (string, temp);
  1555.   free (temp);
  1556. }
  1557.  
  1558. /* Discard text until the desired string is found.  The string is
  1559.    included in the discarded text. */
  1560. discard_until (string)
  1561.      char *string;
  1562. {
  1563.   int temp = search_forward (string, input_text_offset);
  1564.  
  1565.   int tt = (temp < 0) ? size_of_input_text : temp + strlen (string);
  1566.   int from = input_text_offset;
  1567.  
  1568.   /* Find out what line we are on. */
  1569.   while (from != tt)
  1570.     if (input_text[from++] == '\n')
  1571.       line_number++;
  1572.  
  1573.   if (temp < 0)
  1574.     {
  1575.       input_text_offset = size_of_input_text - strlen (string);
  1576.  
  1577.       if (strcmp (string, "\n") != 0)
  1578.     {
  1579.       line_error ("Expected `%s'", string);
  1580.       return;
  1581.     }
  1582.     }
  1583.   else
  1584.     input_text_offset = temp;
  1585.  
  1586.   input_text_offset += strlen (string);
  1587. }
  1588.  
  1589. /* Read characters from the file until we are at MATCH.
  1590.    Place the characters read into STRING.
  1591.    On exit input_text_offset is after the match string.
  1592.    Return the offset where the string starts. */
  1593. int
  1594. get_until (match, string)
  1595.      char *match, **string;
  1596. {
  1597.   int len, current_point, x, new_point, tem;
  1598.  
  1599.   current_point = x = input_text_offset;
  1600.   new_point = search_forward (match, input_text_offset);
  1601.  
  1602.   if (new_point < 0)
  1603.     new_point = size_of_input_text;
  1604.   len = new_point - current_point;
  1605.  
  1606.   /* Keep track of which line number we are at. */
  1607.   tem = new_point + (strlen (match) - 1);
  1608.   while (x != tem)
  1609.     if (input_text[x++] == '\n')
  1610.       line_number++;
  1611.  
  1612.   *string = (char *)xmalloc (len + 1);
  1613.  
  1614.   memcpy (*string, &input_text[current_point], len);
  1615.   (*string)[len] = '\0';
  1616.  
  1617.   /* Now leave input_text_offset in a consistent state. */
  1618.   input_text_offset = tem;
  1619.  
  1620.   if (input_text_offset > size_of_input_text)
  1621.     input_text_offset = size_of_input_text;
  1622.  
  1623.   return (new_point);
  1624. }
  1625.  
  1626. /* Read characters from the file until we are at MATCH or end of line.
  1627.    Place the characters read into STRING.  */
  1628. get_until_in_line (match, string)
  1629.      char *match, **string;
  1630. {
  1631.   int real_bottom, temp;
  1632.  
  1633.   real_bottom = size_of_input_text;
  1634.   temp = search_forward ("\n", input_text_offset);
  1635.  
  1636.   if (temp < 0)
  1637.     temp = size_of_input_text;
  1638.  
  1639.   size_of_input_text = temp;
  1640.   get_until (match, string);
  1641.   size_of_input_text = real_bottom;
  1642. }
  1643.  
  1644. get_rest_of_line (string)
  1645.      char **string;
  1646. {
  1647.   get_until ("\n", string);
  1648.   canon_white (*string);
  1649.  
  1650.   if (curchar () == '\n')    /* as opposed to the end of the file... */
  1651.     {
  1652.       line_number++;
  1653.       input_text_offset++;
  1654.     }
  1655. }
  1656.  
  1657. /* Backup the input pointer to the previous character, keeping track
  1658.    of the current line number. */
  1659. backup_input_pointer ()
  1660. {
  1661.   if (input_text_offset)
  1662.     {
  1663.       input_text_offset--;
  1664.       if (curchar () == '\n')
  1665.     line_number--;
  1666.     }
  1667. }
  1668.  
  1669. /* Read characters from the file until we are at MATCH or closing brace.
  1670.    Place the characters read into STRING.  */
  1671. get_until_in_braces (match, string)
  1672.      char *match, **string;
  1673. {
  1674.   int i, brace = 0;
  1675.   int match_len = strlen (match);
  1676.   char *temp;
  1677.  
  1678.   for (i = input_text_offset; i < size_of_input_text; i++)
  1679.     {
  1680.       if (input_text[i] == '{')
  1681.     brace++;
  1682.       else if (input_text[i] == '}')
  1683.     brace--;
  1684.       else if (input_text[i] == '\n')
  1685.     line_number++;
  1686.  
  1687.       if (brace < 0 ||
  1688.       (brace == 0 && strncmp (input_text + i, match, match_len) == 0))
  1689.     break;
  1690.     }
  1691.  
  1692.   match_len = i - input_text_offset;
  1693.   temp = (char *)xmalloc (2 + match_len);
  1694.   strncpy (temp, input_text + input_text_offset, match_len);
  1695.   temp[match_len] = '\0';
  1696.   input_text_offset = i;
  1697.   *string = temp;
  1698. }
  1699.  
  1700. /* **************************************************************** */
  1701. /*                                    */
  1702. /*            Converting the File                 */
  1703. /*                                    */
  1704. /* **************************************************************** */
  1705.  
  1706. /* Convert the file named by NAME.  The output is saved on the file
  1707.    named as the argument to the @setfilename command. */
  1708. static char *suffixes[] = {
  1709.   "",
  1710.   ".texinfo",
  1711.   ".texi",
  1712.   ".txinfo",
  1713.   (char *)NULL
  1714. };
  1715.  
  1716. convert (name)
  1717.      char *name;
  1718. {
  1719.   char *real_output_filename, *expand_filename (), *filename_part ();
  1720.   char *filename = (char *)xmalloc (strlen (name) + 50);
  1721.   register int i;
  1722.  
  1723.   init_tag_table ();
  1724.   init_indices ();
  1725.   init_internals ();
  1726.   init_paragraph ();
  1727.  
  1728.   /* Try to load the file specified by NAME.  If the file isn't found, and
  1729.      there is no suffix in NAME, then try NAME.texinfo, and NAME.texi. */
  1730.   for (i = 0; suffixes[i]; i++)
  1731.     {
  1732.       strcpy (filename, name);
  1733.       strcat (filename, suffixes[i]);
  1734.  
  1735.       if (find_and_load (filename))
  1736.     break;
  1737.  
  1738.       if (!suffixes[i][0] && strrchr (filename, '.'))
  1739.     {
  1740.       fs_error (filename);
  1741.       free (filename);
  1742.       return;
  1743.     }
  1744.     }
  1745.  
  1746.   if (!suffixes[i])
  1747.     {
  1748.       fs_error (name);
  1749.       free (filename);
  1750.       return;
  1751.     }
  1752.  
  1753.   input_filename = filename;
  1754.  
  1755.   /* Search this file looking for the special string which starts conversion.
  1756.      Once found, we may truly begin. */
  1757.  
  1758.   input_text_offset = search_forward ("@setfilename", 0);
  1759.  
  1760.   if (input_text_offset < 0)
  1761.     {
  1762.       if (!command_output_filename)
  1763.     {
  1764.       error ("No `@setfilename' found in `%s'", name);
  1765.       goto finished;
  1766.     }
  1767.     }
  1768.   else
  1769.     input_text_offset += strlen ("@setfilename");
  1770.  
  1771.   real_output_filename = (char *)NULL;
  1772.  
  1773.   if (!command_output_filename)
  1774.     get_until ("\n", &output_filename);
  1775.   else
  1776.     {
  1777.       if (input_text_offset != -1)
  1778.     discard_until ("\n");
  1779.       else
  1780.     input_text_offset = 0;
  1781.  
  1782.       real_output_filename = output_filename = command_output_filename;
  1783.       command_output_filename = (char *)NULL;
  1784.     }
  1785.  
  1786.   canon_white (output_filename);
  1787.   printf ("Making info file `%s' from `%s'.\n", output_filename, name);
  1788.  
  1789.   if (verbose_mode)
  1790.     fprintf (stderr, "  The input file contains %d characters.\n",
  1791.          size_of_input_text);
  1792.  
  1793.   if (real_output_filename &&
  1794.       strcmp (real_output_filename, "-") == 0)
  1795.     {
  1796.       output_stream = stdout;
  1797.     }
  1798.   else
  1799.     {
  1800.       if (!real_output_filename)
  1801.     real_output_filename = expand_filename (output_filename, name);
  1802.  
  1803.       output_stream = fopen (real_output_filename, "w");
  1804.     }
  1805.  
  1806.   if (output_stream == NULL)
  1807.     {
  1808.       fs_error (real_output_filename);
  1809.       goto finished;
  1810.     }
  1811.  
  1812.   /* Make the displayable filename from output_filename.  Only the base
  1813.      portion of the filename need be displayed. */
  1814.   pretty_output_filename = filename_part (output_filename);
  1815.  
  1816.   /* For this file only, count the number of newlines from the top of
  1817.      the file to here.  This way, we keep track of line numbers for
  1818.      error reporting.  Line_number starts at 1, since the user isn't
  1819.      zero-based. */
  1820.   {
  1821.     int temp = 0;
  1822.     line_number = 1;
  1823.     while (temp != input_text_offset)
  1824.       if (input_text[temp++] == '\n')
  1825.     line_number++;
  1826.   }
  1827.  
  1828.   if (!no_headers)
  1829.     {
  1830.       add_word_args ("This is Info file %s, produced by Makeinfo-%d.%d from ",
  1831.              output_filename, major_version, minor_version);
  1832.       add_word_args ("the input file %s.\n", input_filename);
  1833.     }
  1834.  
  1835.   close_paragraph ();
  1836.   reader_loop ();
  1837.  
  1838. finished:
  1839.   close_paragraph ();
  1840.   flush_file_stack ();
  1841.   if (output_stream != NULL)
  1842.     {
  1843.       output_pending_notes ();
  1844.       free_pending_notes ();
  1845.       if (tag_table != NULL)
  1846.     {
  1847.       tag_table = (TAG_ENTRY *) reverse_list (tag_table);
  1848.       if (!no_headers)
  1849.         write_tag_table ();
  1850.     }
  1851.  
  1852.       if (output_stream != stdout)
  1853.     fclose (output_stream);
  1854.  
  1855.       /* If validating, then validate the entire file right now. */
  1856.       if (validating)
  1857.     validate_file (real_output_filename, tag_table);
  1858.  
  1859.       /* This used to test  && !errors_printed.
  1860.      But some files might have legit warnings.  So split anyway.  */
  1861.       if (splitting)
  1862.     split_file (real_output_filename, 0);
  1863.     }
  1864. }
  1865.  
  1866. free_and_clear (pointer)
  1867.      char **pointer;
  1868. {
  1869.   if ((*pointer) != (char *) NULL)
  1870.     {
  1871.       free (*pointer);
  1872.       *pointer = (char *) NULL;
  1873.     }
  1874. }
  1875.  
  1876.  /* Initialize some state. */
  1877. init_internals ()
  1878. {
  1879.   free_and_clear (¤t_node);
  1880.   free_and_clear (&output_filename);
  1881.   free_and_clear (&command);
  1882.   free_and_clear (&input_filename);
  1883.   free_node_references ();
  1884.   init_insertion_stack ();
  1885.   init_brace_stack ();
  1886.   command_index = 0;
  1887.   in_menu = 0;
  1888. }
  1889.  
  1890. init_paragraph ()
  1891. {
  1892.   free_and_clear (&output_paragraph);
  1893.   output_paragraph = (unsigned char *)xmalloc (paragraph_buffer_len);
  1894.   output_position = 0;
  1895.   output_paragraph[0] = '\0';
  1896.   output_paragraph_offset = 0;
  1897.   output_column = 0;
  1898.   paragraph_is_open = 0;
  1899.   current_indent = 0;
  1900. }
  1901.  
  1902. /* Okay, we are ready to start the conversion.  Call the reader on
  1903.    some text, and fill the text as it is output.  Handle commands by
  1904.    remembering things like open braces and the current file position on a
  1905.    stack, and when the corresponding close brace is found, you can call
  1906.    the function with the proper arguments. */
  1907. reader_loop ()
  1908. {
  1909.   int character;
  1910.   int done = 0;
  1911.   int dash_count = 0;
  1912.  
  1913.   while (!done)
  1914.     {
  1915.       if (input_text_offset >= size_of_input_text)
  1916.     {
  1917.       if (filestack)
  1918.         {
  1919.           free (input_filename);
  1920.           free (input_text);
  1921.           popfile ();
  1922.         }
  1923.       else
  1924.         break;
  1925.     }
  1926.  
  1927.       character = curchar ();
  1928.  
  1929.       if (!in_fixed_width_font &&
  1930.       (character == '\'' || character == '`') &&
  1931.       input_text[input_text_offset + 1] == character)
  1932.     {
  1933.       input_text_offset++;
  1934.       character = '"';
  1935.     }
  1936.  
  1937.       if (character == '-')
  1938.     {
  1939.       dash_count++;
  1940.       if (dash_count == 2 && !in_fixed_width_font)
  1941.         {
  1942.           input_text_offset++;
  1943.           continue;
  1944.         }
  1945.     }
  1946.       else
  1947.     {
  1948.       dash_count = 0;
  1949.     }
  1950.  
  1951.       /* If this is a whitespace character, then check to see if the line
  1952.      is blank.  If so, advance to the carriage return. */
  1953.       if (whitespace (character))
  1954.     {
  1955.       register int i = input_text_offset + 1;
  1956.  
  1957.       while (i < size_of_input_text && whitespace (input_text[i]))
  1958.         i++;
  1959.  
  1960.       if (i == size_of_input_text || input_text[i] == '\n')
  1961.         {
  1962.           if (i == size_of_input_text)
  1963.         i--;
  1964.  
  1965.           input_text_offset = i;
  1966.           character = curchar ();
  1967.         }
  1968.     }
  1969.  
  1970.       if (character == '\n')
  1971.     {
  1972.       line_number++;
  1973.  
  1974.       /* Check for a menu entry here, since the "escape sequence"
  1975.          that begins menu entrys is "\n* ". */
  1976.       if (in_menu && input_text_offset + 1 < size_of_input_text)
  1977.         {
  1978.           char *glean_node_from_menu (), *tem;
  1979.  
  1980.           /* Note that the value of TEM is discarded, since it is
  1981.          gauranteed to be NULL when glean_node_from_menu () is
  1982.          called with a non-zero argument. */
  1983.           tem = glean_node_from_menu (1);
  1984.         }
  1985.     }
  1986.  
  1987.       switch (character)
  1988.     {
  1989.     case COMMAND_PREFIX:
  1990.       read_command ();
  1991.       if (strcmp (command, "bye") == 0)
  1992.         {
  1993.           done = 1;
  1994.           continue;
  1995.         }
  1996.       break;
  1997.  
  1998.     case '{':
  1999.  
  2000.       /* Special case.  I'm not supposed to see this character by itself.
  2001.          If I do, it means there is a syntax error in the input text.
  2002.          Report the error here, but remember this brace on the stack so
  2003.          you can ignore its partner. */
  2004.  
  2005.       line_error ("Misplaced `{'");
  2006.       remember_brace (misplaced_brace);
  2007.  
  2008.       /* Don't advance input_text_offset since this happens in
  2009.          remember_brace ().
  2010.          input_text_offset++;
  2011.            */
  2012.       break;
  2013.  
  2014.     case '}':
  2015.       pop_and_call_brace ();
  2016.       input_text_offset++;
  2017.       break;
  2018.  
  2019.     default:
  2020.       add_char (character);
  2021.       input_text_offset++;
  2022.     }
  2023.     }
  2024. }
  2025.  
  2026. /* Find the command corresponding to STRING.  If the command
  2027.    is found, return a pointer to the data structure.  Otherwise
  2028.    return (-1). */
  2029. COMMAND *
  2030. get_command_entry (string)
  2031.      char *string;
  2032. {
  2033.   register int i;
  2034.  
  2035.   for (i = 0; CommandTable[i].name; i++)
  2036.     if (strcmp (CommandTable[i].name, string) == 0)
  2037.       return (&CommandTable[i]);
  2038.  
  2039.   /* This command is not in our predefined command table.  Perhaps
  2040.      it is a user defined command. */
  2041.   for (i = 0; i < user_command_array_len; i++)
  2042.     if (user_command_array[i] &&
  2043.     (strcmp (user_command_array[i]->name, string) == 0))
  2044.       return (user_command_array[i]);
  2045.  
  2046.   /* Nope, we never heard of this command. */
  2047.   return ((COMMAND *) -1);
  2048. }
  2049.  
  2050. /* input_text_offset is right at the command prefix character.
  2051.    Read the next token to determine what to do. */
  2052. read_command ()
  2053. {
  2054.   COMMAND *entry;
  2055.   input_text_offset++;
  2056.   free_and_clear (&command);
  2057.   command = read_token ();
  2058.  
  2059. #if defined (HAVE_MACROS)
  2060.   /* Check to see if this command is a macro.  If so, execute it here. */
  2061.   {
  2062.     MACRO_DEF *def;
  2063.  
  2064.     def = find_macro (command);
  2065.  
  2066.     if (def)
  2067.       {
  2068.     execute_macro (def);
  2069.     return;
  2070.       }
  2071.   }
  2072. #endif /* HAVE_MACROS */
  2073.  
  2074.   entry = get_command_entry (command);
  2075.  
  2076.   if ((int) entry == -1)
  2077.     {
  2078.       line_error ("Unknown info command `%s'", command);
  2079.       return;
  2080.     }
  2081.  
  2082.   if (entry->argument_in_braces)
  2083.     remember_brace (entry->proc);
  2084.  
  2085.   (*(entry->proc)) (START);
  2086. }
  2087.  
  2088. /* Return the string which invokes PROC; a pointer to a function. */
  2089. char *
  2090. find_proc_name (proc)
  2091.      FUNCTION *proc;
  2092. {
  2093.   register int i;
  2094.  
  2095.   for (i = 0; CommandTable[i].name; i++)
  2096.     if (proc == CommandTable[i].proc)
  2097.       return (CommandTable[i].name);
  2098.   return ("NO_NAME!");
  2099. }
  2100.  
  2101. init_brace_stack ()
  2102. {
  2103.   brace_stack = (BRACE_ELEMENT *) NULL;
  2104. }
  2105.  
  2106. remember_brace (proc)
  2107.      FUNCTION *proc;
  2108. {
  2109.   if (curchar () != '{')
  2110.     line_error ("@%s expected `{..}'", command);
  2111.   else
  2112.     input_text_offset++;
  2113.   remember_brace_1 (proc, output_paragraph_offset);
  2114. }
  2115.  
  2116. /* Remember the current output position here.  Save PROC
  2117.    along with it so you can call it later. */
  2118. remember_brace_1 (proc, position)
  2119.      FUNCTION *proc;
  2120.      int position;
  2121. {
  2122.   BRACE_ELEMENT *new = (BRACE_ELEMENT *) xmalloc (sizeof (BRACE_ELEMENT));
  2123.   new->next = brace_stack;
  2124.   new->proc = proc;
  2125.   new->pos = position;
  2126.   new->line = line_number;
  2127.   brace_stack = new;
  2128. }
  2129.  
  2130. /* Pop the top of the brace stack, and call the associated function
  2131.    with the args END and POS. */
  2132. pop_and_call_brace ()
  2133. {
  2134.   BRACE_ELEMENT *temp;
  2135.   FUNCTION *proc;
  2136.   int pos;
  2137.  
  2138.   if (brace_stack == (BRACE_ELEMENT *) NULL)
  2139.     return (line_error ("Unmatched close brace"));
  2140.  
  2141.   pos = brace_stack->pos;
  2142.   proc = brace_stack->proc;
  2143.   temp = brace_stack->next;
  2144.   free (brace_stack);
  2145.   brace_stack = temp;
  2146.  
  2147.   return ((*proc) (END, pos, output_paragraph_offset));
  2148. }
  2149.  
  2150. /* You call discard_braces () when you shouldn't have any braces on the stack.
  2151.    I used to think that this happens for commands that don't take arguments
  2152.    in braces, but that was wrong because of things like @code{foo @@}.  So now
  2153.    I only detect it at the beginning of nodes. */
  2154. discard_braces ()
  2155. {
  2156.   int temp_line_number = line_number;
  2157.   char *proc_name;
  2158.  
  2159.   if (!brace_stack)
  2160.     return;
  2161.  
  2162.   while (brace_stack)
  2163.     {
  2164.       line_number = brace_stack->line;
  2165.       proc_name = find_proc_name (brace_stack->proc);
  2166.       line_error ("@%s missing close brace", proc_name);
  2167.       line_number = temp_line_number;
  2168.       pop_and_call_brace ();
  2169.     }
  2170. }
  2171.  
  2172. get_char_len (character)
  2173.      int character;
  2174. {
  2175.   /* Return the printed length of the character. */
  2176.   int len;
  2177.  
  2178.   switch (character)
  2179.     {
  2180.     case '\t':
  2181.       len = (output_column + 8) & 0xf7;
  2182.       if (len > fill_column)
  2183.     len = fill_column - output_column;
  2184.       else
  2185.     len = len - output_column;
  2186.       break;
  2187.  
  2188.     case '\n':
  2189.       len = fill_column - output_column;
  2190.       break;
  2191.  
  2192.     default:
  2193.       if (character < ' ')
  2194.     len = 2;
  2195.       else
  2196.     len = 1;
  2197.     }
  2198.   return (len);
  2199. }
  2200.  
  2201. #if defined (HAVE_VARARGS_H) && defined (HAVE_VSPRINTF)
  2202.  
  2203. add_word_args (va_alist)
  2204.      va_dcl
  2205. {
  2206.   char buffer[1000];
  2207.   char *format;
  2208.   va_list args;
  2209.  
  2210.   va_start (args);
  2211.   format = va_arg (args, char *);
  2212.   vsprintf (buffer, format, args);
  2213.   va_end (args);
  2214.   add_word (buffer);
  2215. }
  2216.  
  2217. #else /* !(HAVE_VARARGS_H && HAVE_VSPRINTF) */
  2218.  
  2219. add_word_args (format, arg1, arg2, arg3, arg4, arg5)
  2220.      char *format;
  2221. {
  2222.   char buffer[1000];
  2223.   sprintf (buffer, format, arg1, arg2, arg3, arg4, arg5);
  2224.   add_word (buffer);
  2225. }
  2226.  
  2227. #endif /* !(HAVE_VARARGS_H && HAVE_VSPRINTF) */
  2228.  
  2229. /* Add STRING to output_paragraph. */
  2230. add_word (string)
  2231.      char *string;
  2232. {
  2233.   while (*string)
  2234.     add_char (*string++);
  2235. }
  2236.  
  2237. /* Non-zero if the last character inserted has the syntax class of NEWLINE. */
  2238. int last_char_was_newline = 1;
  2239.  
  2240. /* The actual last inserted character.  Note that this may be something
  2241.    other than NEWLINE even if last_char_was_newline is 1. */
  2242. int last_inserted_character = 0;
  2243.  
  2244. /* Non-zero means that a newline character has already been
  2245.    inserted, so close_paragraph () should insert one less. */
  2246. int line_already_broken = 0;
  2247.  
  2248. /* When non-zero we have finished an insertion (see end_insertion ()) and we
  2249.    want to ignore false continued paragraph closings. */
  2250. int insertion_paragraph_closed = 0;
  2251.  
  2252. /* Add the character to the current paragraph.  If filling_enabled is
  2253.    non-zero, then do filling as well. */
  2254. add_char (character)
  2255.      int character;
  2256. {
  2257.   /* If we are avoiding outputting headers, and we are currently
  2258.      in a menu, then simply return. */
  2259.   if (no_headers && in_menu)
  2260.     return;
  2261.  
  2262.   /* If we are adding a character now, then we don't have to
  2263.      ignore close_paragraph () calls any more. */
  2264.   if (must_start_paragraph && character != '\n')
  2265.     {
  2266.       must_start_paragraph = 0;
  2267.       line_already_broken = 0;    /* The line is no longer broken. */
  2268.       if (current_indent > output_column)
  2269.     {
  2270.       indent (current_indent - output_column);
  2271.       output_column = current_indent;
  2272.     }
  2273.     }
  2274.  
  2275.   if (non_splitting_words && member (character, " \t\n"))
  2276.     character = ' ' | 0x80;
  2277.  
  2278.   insertion_paragraph_closed = 0;
  2279.  
  2280.   switch (character)
  2281.     {
  2282.     case '\n':
  2283.       if (!filling_enabled)
  2284.     {
  2285.       insert ('\n');
  2286.  
  2287.       if (force_flush_right)
  2288.         {
  2289.           close_paragraph ();
  2290.           /* Hack to force single blank lines out in this mode. */
  2291.           flush_output ();
  2292.         }
  2293.  
  2294.       output_column = 0;
  2295.  
  2296.       if (!no_indent && paragraph_is_open)
  2297.         indent (output_column = current_indent);
  2298.       break;
  2299.     }
  2300.       else /* CHARACTER is newline, and filling is enabled. */
  2301.     {
  2302.       if (sentence_ender (last_inserted_character))
  2303.         {
  2304.           insert (' ');
  2305.           output_column++;
  2306.           last_inserted_character = character;
  2307.         }
  2308.     }
  2309.  
  2310.       if (last_char_was_newline)
  2311.     {
  2312.       close_paragraph ();
  2313.       pending_indent = 0;
  2314.     }
  2315.       else
  2316.     {
  2317.       last_char_was_newline = 1;
  2318.       insert (' ');
  2319.       output_column++;
  2320.     }
  2321.       break;
  2322.  
  2323.     default:
  2324.       {
  2325.     int len = get_char_len (character);
  2326.     int suppress_insert = 0;
  2327.  
  2328.     if ((character == ' ') && (last_char_was_newline))
  2329.       {
  2330.         if (!paragraph_is_open)
  2331.           {
  2332.         pending_indent++;
  2333.         return;
  2334.           }
  2335.       }
  2336.  
  2337.     if (!paragraph_is_open)
  2338.       {
  2339.         start_paragraph ();
  2340.  
  2341.         /* If the paragraph is supposed to be indented a certain way,
  2342.            then discard all of the pending whitespace.  Otherwise, we
  2343.            let the whitespace stay. */
  2344.         if (!paragraph_start_indent)
  2345.           indent (pending_indent);
  2346.         pending_indent = 0;
  2347.       }
  2348.  
  2349.     if ((output_column += len) > fill_column)
  2350.       {
  2351.         if (filling_enabled)
  2352.           {
  2353.         int temp = output_paragraph_offset;
  2354.         while (--temp > 0 && output_paragraph[temp] != '\n')
  2355.           {
  2356.             /* If we have found a space, we have the place to break
  2357.                the line. */
  2358.             if (output_paragraph[temp] == ' ')
  2359.               {
  2360.             /* Remove trailing whitespace from output. */
  2361.             while (temp && whitespace (output_paragraph[temp - 1]))
  2362.               temp--;
  2363.  
  2364.             output_paragraph[temp++] = '\n';
  2365.  
  2366.             /* We have correctly broken the line where we want
  2367.                to.  What we don't want is spaces following where
  2368.                we have decided to break the line.  We get rid of
  2369.                them. */
  2370.             {
  2371.               int t1 = temp;
  2372.  
  2373.               for (;; t1++)
  2374.                 {
  2375.                   if (t1 == output_paragraph_offset)
  2376.                 {
  2377.                   if (whitespace (character))
  2378.                     suppress_insert = 1;
  2379.                   break;
  2380.                 }
  2381.                   if (!whitespace (output_paragraph[t1]))
  2382.                 break;
  2383.                 }
  2384.  
  2385.               if (t1 != temp)
  2386.                 {
  2387.                   strncpy ((char *) &output_paragraph[temp],
  2388.                        (char *) &output_paragraph[t1],
  2389.                        (output_paragraph_offset - t1));
  2390.                   output_paragraph_offset -= (t1 - temp);
  2391.                 }
  2392.             }
  2393.  
  2394.             /* Filled, but now indent if that is right. */
  2395.             if (indented_fill && current_indent)
  2396.               {
  2397.                 int buffer_len = ((output_paragraph_offset - temp)
  2398.                           + current_indent);
  2399.                 char *temp_buffer = (char *)xmalloc (buffer_len);
  2400.                 int indentation = 0;
  2401.  
  2402.                 /* We have to shift any markers that are in
  2403.                    front of the wrap point. */
  2404.                 {
  2405.                   register BRACE_ELEMENT *stack = brace_stack;
  2406.  
  2407.                   while (stack)
  2408.                 {
  2409.                   if (stack->pos >= temp)
  2410.                     stack->pos += current_indent;
  2411.                   stack = stack->next;
  2412.                 }
  2413.                 }
  2414.  
  2415.                 while (current_indent > 0 &&
  2416.                    indentation != current_indent)
  2417.                   temp_buffer[indentation++] = ' ';
  2418.  
  2419.                 strncpy ((char *) &temp_buffer[current_indent],
  2420.                      (char *) &output_paragraph[temp],
  2421.                      buffer_len - current_indent);
  2422.  
  2423.                 if (output_paragraph_offset + buffer_len
  2424.                 >= paragraph_buffer_len)
  2425.                   {
  2426.                 unsigned char *tt = xrealloc
  2427.                   (output_paragraph,
  2428.                    (paragraph_buffer_len += buffer_len));
  2429.                 output_paragraph = tt;
  2430.                   }
  2431.                 strncpy ((char *) &output_paragraph[temp],
  2432.                      temp_buffer, buffer_len);
  2433.                 output_paragraph_offset += current_indent;
  2434.                 free (temp_buffer);
  2435.               }
  2436.             output_column = 0;
  2437.             while (temp < output_paragraph_offset)
  2438.               output_column +=
  2439.                 get_char_len (output_paragraph[temp++]);
  2440.             output_column += len;
  2441.             break;
  2442.               }
  2443.           }
  2444.           }
  2445.       }
  2446.  
  2447.     if (!suppress_insert)
  2448.       {
  2449.         insert (character);
  2450.         last_inserted_character = character;
  2451.       }
  2452.     last_char_was_newline = 0;
  2453.     line_already_broken = 0;
  2454.       }
  2455.     }
  2456. }
  2457.  
  2458. /* Insert CHARACTER into OUTPUT_PARAGRAPH. */
  2459. insert (character)
  2460.      int character;
  2461. {
  2462.   output_paragraph[output_paragraph_offset++] = character;
  2463.   if (output_paragraph_offset == paragraph_buffer_len)
  2464.     {
  2465.       output_paragraph =
  2466.     xrealloc (output_paragraph, (paragraph_buffer_len += 100));
  2467.     }
  2468. }
  2469.  
  2470. /* Remove upto COUNT characters of whitespace from the
  2471.    the current output line.  If COUNT is less than zero,
  2472.    then remove until none left. */
  2473. kill_self_indent (count)
  2474.      int count;
  2475. {
  2476.   /* Handle infinite case first. */
  2477.   if (count < 0)
  2478.     {
  2479.       output_column = 0;
  2480.       while (output_paragraph_offset)
  2481.     {
  2482.       if (whitespace (output_paragraph[output_paragraph_offset - 1]))
  2483.         output_paragraph_offset--;
  2484.       else
  2485.         break;
  2486.     }
  2487.     }
  2488.   else
  2489.     {
  2490.       while (output_paragraph_offset && count--)
  2491.     if (whitespace (output_paragraph[output_paragraph_offset - 1]))
  2492.       output_paragraph_offset--;
  2493.     else
  2494.       break;
  2495.     }
  2496. }
  2497.  
  2498. /* Non-zero means do not honor calls to flush_output (). */
  2499. static int flushing_ignored = 0;
  2500.  
  2501. /* Prevent calls to flush_output () from having any effect. */
  2502. inhibit_output_flushing ()
  2503. {
  2504.   flushing_ignored++;
  2505. }
  2506.  
  2507. /* Allow calls to flush_output () to write the paragraph data. */
  2508. uninhibit_output_flushing ()
  2509. {
  2510.   flushing_ignored--;
  2511. }
  2512.  
  2513. flush_output ()
  2514. {
  2515.   register int i;
  2516.  
  2517.   if (!output_paragraph_offset || flushing_ignored)
  2518.     return;
  2519.  
  2520.   for (i = 0; i < output_paragraph_offset; i++)
  2521.     {
  2522.       if (output_paragraph[i] == (unsigned char)(' ' | 0x80) ||
  2523.       output_paragraph[i] == (unsigned char)('\t' | 0x80) ||
  2524.       output_paragraph[i] == (unsigned char)('\n' | 0x80) ||
  2525.       sentence_ender (UNMETA (output_paragraph[i])))
  2526.     output_paragraph[i] &= 0x7f;
  2527.     }
  2528.  
  2529.   fwrite (output_paragraph, 1, output_paragraph_offset, output_stream);
  2530.  
  2531.   output_position += output_paragraph_offset;
  2532.   output_paragraph_offset = 0;
  2533. }
  2534.  
  2535. /* How to close a paragraph controlling the number of lines between
  2536.    this one and the last one. */
  2537.  
  2538. /* Paragraph spacing is controlled by this variable.  It is the number of
  2539.    blank lines that you wish to appear between paragraphs.  A value of
  2540.    1 creates a single blank line between paragraphs. */
  2541. int paragraph_spacing = DEFAULT_PARAGRAPH_SPACING;
  2542.  
  2543. /* Close the current paragraph, leaving no blank lines between them. */
  2544. close_single_paragraph ()
  2545. {
  2546.   close_paragraph_with_lines (0);
  2547. }
  2548.  
  2549. /* Close a paragraph after an insertion has ended. */
  2550. close_insertion_paragraph ()
  2551. {
  2552.   if (!insertion_paragraph_closed)
  2553.     {
  2554.       /* Close the current paragraph, breaking the line. */
  2555.       close_single_paragraph ();
  2556.  
  2557.       /* Start a new paragraph here, inserting whatever indention is correct
  2558.      for the now current insertion level (one above the one that we are
  2559.      ending). */
  2560.       start_paragraph ();
  2561.  
  2562.       /* Tell close_paragraph () that the previous line has already been
  2563.      broken, so it should insert one less newline. */
  2564.       line_already_broken = 1;
  2565.  
  2566.       /* Let functions such as add_char () know that we have already found a
  2567.      newline. */
  2568.       ignore_blank_line ();
  2569.     }
  2570.   else
  2571.     {
  2572.       /* If the insertion paragraph is closed already, then we are seeing
  2573.      two `@end' commands in a row.  Note that the first one we saw was
  2574.      handled in the first part of this if-then-else clause, and at that
  2575.      time start_paragraph () was called, partially to handle the proper
  2576.      indentation of the current line.  However, the indentation level
  2577.      may have just changed again, so we may have to outdent the current
  2578.      line to the new indentation level. */
  2579.       if (current_indent < output_column)
  2580.     kill_self_indent (output_column - current_indent);
  2581.     }
  2582.  
  2583.   insertion_paragraph_closed = 1;
  2584. }
  2585.  
  2586. close_paragraph_with_lines (lines)
  2587.      int lines;
  2588. {
  2589.   int old_spacing = paragraph_spacing;
  2590.   paragraph_spacing = lines;
  2591.   close_paragraph ();
  2592.   paragraph_spacing = old_spacing;
  2593. }
  2594.  
  2595. /* Close the currently open paragraph. */
  2596. close_paragraph ()
  2597. {
  2598.   register int i;
  2599.  
  2600.   /* The insertion paragraph is no longer closed. */
  2601.   insertion_paragraph_closed = 0;
  2602.  
  2603.   if (paragraph_is_open && !must_start_paragraph)
  2604.     {
  2605.       register int tindex, c;
  2606.  
  2607.       tindex = output_paragraph_offset;
  2608.  
  2609.       /* Back up to last non-newline/space character, forcing all such
  2610.      subsequent characters to be newlines.  This isn't strictly
  2611.      necessary, but a couple of functions use the presence of a newline
  2612.      to make decisions. */
  2613.       for (tindex = output_paragraph_offset - 1; tindex >= 0; --tindex)
  2614.     {
  2615.       c = output_paragraph[tindex];
  2616.  
  2617.       if (c == ' '|| c == '\n')
  2618.         output_paragraph[tindex] = '\n';
  2619.       else
  2620.         break;
  2621.     }
  2622.  
  2623.       /* All trailing whitespace is ignored. */
  2624.       output_paragraph_offset = ++tindex;
  2625.  
  2626.       /* Break the line if that is appropriate. */
  2627.       if (paragraph_spacing >= 0)
  2628.     insert ('\n');
  2629.  
  2630.       /* Add as many blank lines as is specified in PARAGRAPH_SPACING. */
  2631.       if (!force_flush_right)
  2632.     {
  2633.       for (i = 0; i < (paragraph_spacing - line_already_broken); i++)
  2634.         insert ('\n');
  2635.     }
  2636.  
  2637.       /* If we are doing flush right indentation, then do it now
  2638.      on the paragraph (really a single line). */
  2639.       if (force_flush_right)
  2640.     do_flush_right_indentation ();
  2641.  
  2642.       flush_output ();
  2643.       paragraph_is_open = 0;
  2644.       no_indent = 0;
  2645.       output_column = 0;
  2646.     }
  2647.   ignore_blank_line ();
  2648. }
  2649.  
  2650. /* Make the last line just read look as if it were only a newline. */
  2651. ignore_blank_line ()
  2652. {
  2653.   last_inserted_character = '\n';
  2654.   last_char_was_newline = 1;
  2655. }
  2656.  
  2657. /* Align the end of the text in output_paragraph with fill_column. */
  2658. do_flush_right_indentation ()
  2659. {
  2660.   char *temp;
  2661.   int temp_len;
  2662.  
  2663.   kill_self_indent (-1);
  2664.  
  2665.   if (output_paragraph[0] != '\n')
  2666.     {
  2667.       output_paragraph[output_paragraph_offset] = '\0';
  2668.  
  2669.       if (output_paragraph_offset < fill_column)
  2670.     {
  2671.       register int i;
  2672.  
  2673.       if (fill_column >= paragraph_buffer_len)
  2674.         output_paragraph =
  2675.           xrealloc (output_paragraph,
  2676.             (paragraph_buffer_len += fill_column));
  2677.  
  2678.       temp_len = strlen ((char *)output_paragraph);
  2679.       temp = (char *)xmalloc (temp_len + 1);
  2680.       memcpy (temp, (char *)output_paragraph, temp_len);
  2681.  
  2682.       for (i = 0; i < fill_column - output_paragraph_offset; i++)
  2683.         output_paragraph[i] = ' ';
  2684.  
  2685.       memcpy ((char *)output_paragraph + i, temp, temp_len);
  2686.       free (temp);
  2687.       output_paragraph_offset = fill_column;
  2688.     }
  2689.     }
  2690. }
  2691.  
  2692. /* Begin a new paragraph. */
  2693. start_paragraph ()
  2694. {
  2695.   /* First close existing one. */
  2696.   if (paragraph_is_open)
  2697.     close_paragraph ();
  2698.  
  2699.   /* In either case, the insertion paragraph is no longer closed. */
  2700.   insertion_paragraph_closed = 0;
  2701.  
  2702.   /* However, the paragraph is open! */
  2703.   paragraph_is_open = 1;
  2704.  
  2705.   /* If we MUST_START_PARAGRAPH, that simply means that start_paragraph ()
  2706.      had to be called before we would allow any other paragraph operations
  2707.      to have an effect. */
  2708.   if (!must_start_paragraph)
  2709.     {
  2710.       int amount_to_indent = 0;
  2711.  
  2712.       /* If doing indentation, then insert the appropriate amount. */
  2713.       if (!no_indent)
  2714.     {
  2715.       if (inhibit_paragraph_indentation)
  2716.         {
  2717.           amount_to_indent = current_indent;
  2718.           if (inhibit_paragraph_indentation < 0)
  2719.         inhibit_paragraph_indentation++;
  2720.         }
  2721.       else if (paragraph_start_indent < 0)
  2722.         amount_to_indent = current_indent;
  2723.       else
  2724.         amount_to_indent = current_indent + paragraph_start_indent;
  2725.  
  2726.       if (amount_to_indent >= output_column)
  2727.         {
  2728.           amount_to_indent -= output_column;
  2729.           indent (amount_to_indent);
  2730.           output_column += amount_to_indent;
  2731.         }
  2732.     }
  2733.     }
  2734.   else
  2735.     must_start_paragraph = 0;
  2736. }
  2737.  
  2738. /* Insert the indentation specified by AMOUNT. */
  2739. indent (amount)
  2740.      int amount;
  2741. {
  2742.   register BRACE_ELEMENT *elt = brace_stack;
  2743.  
  2744.   /* For every START_POS saved within the brace stack which will be affected
  2745.      by this indentation, bump that start pos forward. */
  2746.   while (elt)
  2747.     {
  2748.       if (elt->pos >= output_paragraph_offset)
  2749.     elt->pos += amount;
  2750.       elt = elt->next;
  2751.     }
  2752.  
  2753.   while (--amount >= 0)
  2754.     insert (' ');
  2755. }
  2756.  
  2757. /* Search forward for STRING in input_text.
  2758.    FROM says where where to start. */
  2759. search_forward (string, from)
  2760.      char *string;
  2761.      int from;
  2762. {
  2763.   int len = strlen (string);
  2764.  
  2765.   while (from < size_of_input_text)
  2766.     {
  2767.       if (strncmp (input_text + from, string, len) == 0)
  2768.     return (from);
  2769.       from++;
  2770.     }
  2771.   return (-1);
  2772. }
  2773.  
  2774. /* Whoops, Unix doesn't have stricmp. */
  2775.  
  2776. /* Case independent string compare. */
  2777. stricmp (string1, string2)
  2778.      char *string1, *string2;
  2779. {
  2780.   char ch1, ch2;
  2781.  
  2782.   for (;;)
  2783.     {
  2784.       ch1 = *string1++;
  2785.       ch2 = *string2++;
  2786.  
  2787.       if (!(ch1 | ch2))
  2788.     return (0);
  2789.  
  2790.       ch1 = coerce_to_upper (ch1);
  2791.       ch2 = coerce_to_upper (ch2);
  2792.  
  2793.       if (ch1 != ch2)
  2794.     return (ch1 - ch2);
  2795.     }
  2796. }
  2797.  
  2798. enum insertion_type { menu, quotation, lisp, smalllisp, example,
  2799.   smallexample, display, itemize, format, enumerate, cartouche, table,
  2800.   ftable, vtable, group, ifinfo, flushleft, flushright, ifset, ifclear, deffn,
  2801.   defun, defmac, defspec, defvr, defvar, defopt, deftypefn,
  2802.   deftypefun, deftypevr, deftypevar, defcv, defivar, defop, defmethod,
  2803.   deftypemethod, deftp, bad_type };
  2804.  
  2805. char *insertion_type_names[] = { "menu", "quotation", "lisp",
  2806.   "smalllisp", "example", "smallexample", "display", "itemize",
  2807.   "format", "enumerate", "cartouche", "table", "ftable", "vtable", "group",
  2808.   "ifinfo", "flushleft", "flushright", "ifset", "ifclear", "deffn",
  2809.   "defun", "defmac", "defspec", "defvr", "defvar", "defopt",
  2810.   "deftypefn", "deftypefun", "deftypevr", "deftypevar", "defcv",
  2811.   "defivar", "defop", "defmethod", "deftypemethod", "deftp",
  2812.   "bad_type" };
  2813.  
  2814. int insertion_level = 0;
  2815. typedef struct istack_elt
  2816. {
  2817.   struct istack_elt *next;
  2818.   char *item_function;
  2819.   int line_number;
  2820.   int filling_enabled;
  2821.   int indented_fill;
  2822.   enum insertion_type insertion;
  2823.   int inhibited;
  2824. } INSERTION_ELT;
  2825.  
  2826. INSERTION_ELT *insertion_stack = (INSERTION_ELT *) NULL;
  2827.  
  2828. init_insertion_stack ()
  2829. {
  2830.   insertion_stack = (INSERTION_ELT *) NULL;
  2831. }
  2832.  
  2833. /* Return the type of the current insertion. */
  2834. enum insertion_type
  2835. current_insertion_type ()
  2836. {
  2837.   if (!insertion_level)
  2838.     return (bad_type);
  2839.   else
  2840.     return (insertion_stack->insertion);
  2841. }
  2842.  
  2843. /* Return a pointer to the string which is the function to wrap around
  2844.    items. */
  2845. char *
  2846. current_item_function ()
  2847. {
  2848.   register int level, done;
  2849.   register INSERTION_ELT *elt;
  2850.  
  2851.   level = insertion_level;
  2852.   elt = insertion_stack;
  2853.   done = 0;
  2854.  
  2855.   /* Skip down through the stack until we find a non-conditional insertion. */
  2856.   while (!done)
  2857.     {
  2858.       switch (elt->insertion)
  2859.     {
  2860.     case ifinfo:
  2861.     case ifset:
  2862.     case ifclear:
  2863.     case cartouche:
  2864.       elt = elt->next;
  2865.       level--;
  2866.       break;
  2867.  
  2868.     default:
  2869.       done = 1;
  2870.     }
  2871.     }
  2872.  
  2873.   if (!level)
  2874.     return ((char *) NULL);
  2875.   else
  2876.     return (elt->item_function);
  2877. }
  2878.  
  2879. char *
  2880. get_item_function ()
  2881. {
  2882.   char *item_function;
  2883.   get_rest_of_line (&item_function);
  2884.   backup_input_pointer ();
  2885.   canon_white (item_function);
  2886.   return (item_function);
  2887. }
  2888.  
  2889.  /* Push the state of the current insertion on the stack. */
  2890. push_insertion (type, item_function)
  2891.      enum insertion_type type;
  2892.      char *item_function;
  2893. {
  2894.   INSERTION_ELT *new = (INSERTION_ELT *) xmalloc (sizeof (INSERTION_ELT));
  2895.  
  2896.   new->item_function = item_function;
  2897.   new->filling_enabled = filling_enabled;
  2898.   new->indented_fill = indented_fill;
  2899.   new->insertion = type;
  2900.   new->line_number = line_number;
  2901.   new->inhibited = inhibit_paragraph_indentation;
  2902.   new->next = insertion_stack;
  2903.   insertion_stack = new;
  2904.   insertion_level++;
  2905. }
  2906.  
  2907.  /* Pop the value on top of the insertion stack into the
  2908.     global variables. */
  2909. pop_insertion ()
  2910. {
  2911.   INSERTION_ELT *temp = insertion_stack;
  2912.  
  2913.   if (temp == (INSERTION_ELT *) NULL)
  2914.     return;
  2915.  
  2916.   inhibit_paragraph_indentation = temp->inhibited;
  2917.   filling_enabled = temp->filling_enabled;
  2918.   indented_fill = temp->indented_fill;
  2919.   free_and_clear (&(temp->item_function));
  2920.   insertion_stack = insertion_stack->next;
  2921.   free (temp);
  2922.   insertion_level--;
  2923. }
  2924.  
  2925.  /* Return a pointer to the print name of this
  2926.     enumerated type. */
  2927. char *
  2928. insertion_type_pname (type)
  2929.      enum insertion_type type;
  2930. {
  2931.   if ((int) type < (int) bad_type)
  2932.     return (insertion_type_names[(int) type]);
  2933.   else
  2934.     return ("Broken-Type in insertion_type_pname");
  2935. }
  2936.  
  2937. /* Return the insertion_type associated with NAME.
  2938.    If the type is not one of the known ones, return BAD_TYPE. */
  2939. enum insertion_type
  2940. find_type_from_name (name)
  2941.      char *name;
  2942. {
  2943.   int index = 0;
  2944.   while (index < (int) bad_type)
  2945.     {
  2946.       if (strcmp (name, insertion_type_names[index]) == 0)
  2947.     return (enum insertion_type) index;
  2948.       index++;
  2949.     }
  2950.   return (bad_type);
  2951. }
  2952.  
  2953. do_nothing ()
  2954. {
  2955. }
  2956.  
  2957. int
  2958. defun_insertion (type)
  2959.      enum insertion_type type;
  2960. {
  2961.   return
  2962.     ((type == deffn)
  2963.      || (type == defun)
  2964.      || (type == defmac)
  2965.      || (type == defspec)
  2966.      || (type == defvr)
  2967.      || (type == defvar)
  2968.      || (type == defopt)
  2969.      || (type == deftypefn)
  2970.      || (type == deftypefun)
  2971.      || (type == deftypevr)
  2972.      || (type == deftypevar)
  2973.      || (type == defcv)
  2974.      || (type == defivar)
  2975.      || (type == defop)
  2976.      || (type == defmethod)
  2977.      || (type == deftypemethod)
  2978.      || (type == deftp));
  2979. }
  2980.  
  2981. /* MAX_NS is the maximum nesting level for enumerations.  I picked 100
  2982.    which seemed reasonable.  This doesn't control the number of items,
  2983.    just the number of nested lists. */
  2984. #define max_stack_depth 100
  2985. #define ENUM_DIGITS 1
  2986. #define ENUM_ALPHA  2
  2987. typedef struct {
  2988.   int enumtype;
  2989.   int enumval;
  2990. } DIGIT_ALPHA;
  2991.  
  2992. DIGIT_ALPHA enumstack[max_stack_depth];
  2993. int enumstack_offset = 0;
  2994. int current_enumval = 1;
  2995. int current_enumtype = ENUM_DIGITS;
  2996. char *enumeration_arg = (char *)NULL;
  2997.  
  2998. start_enumerating (at, type)
  2999.      int at, type;
  3000. {
  3001.   if ((enumstack_offset + 1) == max_stack_depth)
  3002.     {
  3003.       line_error ("Enumeration stack overflow");
  3004.       return;
  3005.     }
  3006.   enumstack[enumstack_offset].enumtype = current_enumtype;
  3007.   enumstack[enumstack_offset].enumval = current_enumval;
  3008.   enumstack_offset++;
  3009.   current_enumval = at;
  3010.   current_enumtype = type;
  3011. }
  3012.  
  3013. stop_enumerating ()
  3014. {
  3015.   --enumstack_offset;
  3016.   if (enumstack_offset < 0)
  3017.     enumstack_offset = 0;
  3018.  
  3019.   current_enumval = enumstack[enumstack_offset].enumval;
  3020.   current_enumtype = enumstack[enumstack_offset].enumtype;
  3021. }
  3022.  
  3023. /* Place a letter or digits into the output stream. */
  3024. enumerate_item ()
  3025. {
  3026.   char temp[10];
  3027.  
  3028.   if (current_enumtype == ENUM_ALPHA)
  3029.     {
  3030.       if (current_enumval == ('z' + 1) || current_enumval == ('Z' + 1))
  3031.     {
  3032.       current_enumval = ((current_enumval - 1) == 'z' ? 'a' : 'A');
  3033.       warning ("Lettering overflow, restarting at %c", current_enumval);
  3034.     }
  3035.       sprintf (temp, "%c. ", current_enumval);
  3036.     }
  3037.   else
  3038.     sprintf (temp, "%d. ", current_enumval);
  3039.  
  3040.   indent (output_column += (current_indent - strlen (temp)));
  3041.   add_word (temp);
  3042.   current_enumval++;
  3043. }
  3044.  
  3045. /* This is where the work for all the "insertion" style
  3046.    commands is done.  A huge switch statement handles the
  3047.    various setups, and generic code is on both sides. */
  3048. begin_insertion (type)
  3049.      enum insertion_type type;
  3050. {
  3051.   int no_discard = 0;
  3052.  
  3053.   if (defun_insertion (type))
  3054.     {
  3055.       push_insertion (type, savestring (""));
  3056.       no_discard++;
  3057.     }
  3058.   else
  3059.     push_insertion (type, get_item_function ());
  3060.  
  3061.   switch (type)
  3062.     {
  3063.     case menu:
  3064.       if (!no_headers)
  3065.     close_paragraph ();
  3066.  
  3067.       filling_enabled = no_indent = 0;
  3068.       inhibit_paragraph_indentation = 1;
  3069.  
  3070.       if (!no_headers)
  3071.     add_word ("* Menu:\n");
  3072.  
  3073.       in_menu++;
  3074.       no_discard++;
  3075.       break;
  3076.  
  3077.       /* I think @quotation is meant to do filling.
  3078.      If you don't want filling, then use @example. */
  3079.     case quotation:
  3080.       close_single_paragraph ();
  3081.       last_char_was_newline = no_indent = 0;
  3082.       indented_fill = filling_enabled = 1;
  3083.       inhibit_paragraph_indentation = 1;
  3084.       current_indent += default_indentation_increment;
  3085.       break;
  3086.  
  3087.     case display:
  3088.     case example:
  3089.     case smallexample:
  3090.     case lisp:
  3091.     case smalllisp:
  3092.       /* Just like @example, but no indentation. */
  3093.     case format:
  3094.  
  3095.       close_single_paragraph ();
  3096.       inhibit_paragraph_indentation = 1;
  3097.       in_fixed_width_font++;
  3098.       filling_enabled = 0;
  3099.       last_char_was_newline = 0;
  3100.  
  3101.       if (type != format)
  3102.     current_indent += default_indentation_increment;
  3103.  
  3104.       break;
  3105.  
  3106.     case table:
  3107.     case ftable:
  3108.     case vtable:
  3109.     case itemize:
  3110.       close_single_paragraph ();
  3111.       current_indent += default_indentation_increment;
  3112.       filling_enabled = indented_fill = 1;
  3113. #if defined (INDENT_PARAGRAPHS_IN_TABLE)
  3114.       inhibit_paragraph_indentation = 0;
  3115. #else
  3116.       inhibit_paragraph_indentation = 1;
  3117. #endif /* !INDENT_PARAGRAPHS_IN_TABLE */
  3118.  
  3119.       /* Make things work for losers who forget the itemize syntax. */
  3120.       if (type == itemize)
  3121.     {
  3122.       if (!(*insertion_stack->item_function))
  3123.         {
  3124.           free (insertion_stack->item_function);
  3125.           insertion_stack->item_function = savestring ("@bullet");
  3126.         }
  3127.     }
  3128.       break;
  3129.  
  3130.     case enumerate:
  3131.       close_single_paragraph ();
  3132.       no_indent = 0;
  3133. #if defined (INDENT_PARAGRAPHS_IN_TABLE)
  3134.       inhibit_paragraph_indentation = 0;
  3135. #else
  3136.       inhibit_paragraph_indentation = 1;
  3137. #endif /* !INDENT_PARAGRAPHS_IN_TABLE */
  3138.  
  3139.       current_indent += default_indentation_increment;
  3140.       filling_enabled = indented_fill = 1;
  3141.  
  3142.       if (isdigit (*enumeration_arg))
  3143.     start_enumerating (atoi (enumeration_arg), ENUM_DIGITS);
  3144.       else
  3145.     start_enumerating (*enumeration_arg, ENUM_ALPHA);
  3146.       break;
  3147.  
  3148.       /* Does nothing special in makeinfo. */
  3149.     case group:
  3150.       /* Only close the paragraph if we are not inside of an @example. */
  3151.       if (!insertion_stack->next ||
  3152.       insertion_stack->next->insertion != example)
  3153.     close_single_paragraph ();
  3154.       break;
  3155.  
  3156.       /* Insertions that are no-ops in info, but do something in TeX. */
  3157.     case ifinfo:
  3158.     case ifset:
  3159.     case ifclear:
  3160.     case cartouche:
  3161.       if (in_menu)
  3162.     no_discard++;
  3163.       break;
  3164.  
  3165.     case deffn:
  3166.     case defun:
  3167.     case defmac:
  3168.     case defspec:
  3169.     case defvr:
  3170.     case defvar:
  3171.     case defopt:
  3172.     case deftypefn:
  3173.     case deftypefun:
  3174.     case deftypevr:
  3175.     case deftypevar:
  3176.     case defcv:
  3177.     case defivar:
  3178.     case defop:
  3179.     case defmethod:
  3180.     case deftypemethod:
  3181.     case deftp:
  3182.       inhibit_paragraph_indentation = 1;
  3183.       filling_enabled = indented_fill = 1;
  3184.       current_indent += default_indentation_increment;
  3185.       no_indent = 0;
  3186.       break;
  3187.  
  3188.     case flushleft:
  3189.       close_single_paragraph ();
  3190.       inhibit_paragraph_indentation = 1;
  3191.       filling_enabled = indented_fill = no_indent = 0;
  3192.       break;
  3193.  
  3194.     case flushright:
  3195.       close_single_paragraph ();
  3196.       filling_enabled = indented_fill = no_indent = 0;
  3197.       inhibit_paragraph_indentation = 1;
  3198.       force_flush_right++;
  3199.       break;
  3200.     }
  3201.  
  3202.   if (!no_discard)
  3203.     discard_until ("\n");
  3204. }
  3205.  
  3206. /* Try to end the insertion with the specified TYPE.
  3207.    TYPE, with a value of bad_type,  gets translated to match
  3208.    the value currently on top of the stack.
  3209.    Otherwise, if TYPE doesn't match the top of the insertion stack,
  3210.    give error. */
  3211. end_insertion (type)
  3212.      enum insertion_type type;
  3213. {
  3214.   enum insertion_type temp_type;
  3215.  
  3216.   if (!insertion_level)
  3217.     return;
  3218.  
  3219.   temp_type = current_insertion_type ();
  3220.  
  3221.   if (type == bad_type)
  3222.     type = temp_type;
  3223.  
  3224.   if (type != temp_type)
  3225.     {
  3226.       line_error
  3227.     ("`%cend' expected `%s', but saw `%s'.", COMMAND_PREFIX,
  3228.      insertion_type_pname (temp_type), insertion_type_pname (type));
  3229.       return;
  3230.     }
  3231.  
  3232.   pop_insertion ();
  3233.  
  3234.   switch (type)
  3235.     {
  3236.       /* Insertions which have no effect on paragraph formatting. */
  3237.     case ifinfo:
  3238.     case ifset:
  3239.     case ifclear:
  3240.       break;
  3241.  
  3242.     case menu:
  3243.       in_menu--;        /* No longer hacking menus. */
  3244.       if (!no_headers)
  3245.     close_insertion_paragraph ();
  3246.       break;
  3247.  
  3248.     case enumerate:
  3249.       stop_enumerating ();
  3250.       close_insertion_paragraph ();
  3251.       current_indent -= default_indentation_increment;
  3252.       break;
  3253.  
  3254.     case flushleft:
  3255.     case group:
  3256.     case cartouche:
  3257.       close_insertion_paragraph ();
  3258.       break;
  3259.  
  3260.     case format:
  3261.     case display:
  3262.     case example:
  3263.     case smallexample:
  3264.     case lisp:
  3265.     case smalllisp:
  3266.     case quotation:
  3267.  
  3268.       /* @quotation is the only one of the above without a fixed width
  3269.      font. */
  3270.       if (type != quotation)
  3271.     in_fixed_width_font--;
  3272.  
  3273.       /* @format is the only fixed_width insertion without a change
  3274.      in indentation. */
  3275.       if (type != format)
  3276.     current_indent -= default_indentation_increment;
  3277.  
  3278.       /* The ending of one of these insertions always marks the
  3279.      start of a new paragraph. */
  3280.       close_insertion_paragraph ();
  3281.       break;
  3282.  
  3283.     case table:
  3284.     case ftable:
  3285.     case vtable:
  3286.     case itemize:
  3287.       current_indent -= default_indentation_increment;
  3288.       break;
  3289.  
  3290.     case flushright:
  3291.       force_flush_right--;
  3292.       close_insertion_paragraph ();
  3293.       break;
  3294.  
  3295.       /* Handle the @defun style insertions with a default clause. */
  3296.     default:
  3297.       current_indent -= default_indentation_increment;
  3298.       close_insertion_paragraph ();
  3299.       break;
  3300.     }
  3301. }
  3302.  
  3303. /* Insertions cannot cross certain boundaries, such as node beginnings.  In
  3304.    code that creates such boundaries, you should call discard_insertions ()
  3305.    before doing anything else.  It prints the errors for you, and cleans up
  3306.    the insertion stack. */
  3307. discard_insertions ()
  3308. {
  3309.   int real_line_number = line_number;
  3310.   while (insertion_stack)
  3311.     {
  3312.       if (insertion_stack->insertion == ifinfo ||
  3313.       insertion_stack->insertion == ifset ||
  3314.       insertion_stack->insertion == ifclear ||
  3315.       insertion_stack->insertion == cartouche)
  3316.     break;
  3317.       else
  3318.     {
  3319.       char *offender = (char *)
  3320.         insertion_type_pname (insertion_stack->insertion);
  3321.  
  3322.       line_number = insertion_stack->line_number;
  3323.       line_error ("This `%s' doesn't have a matching `%cend %s'", offender,
  3324.               COMMAND_PREFIX, offender);
  3325.       pop_insertion ();
  3326.     }
  3327.     }
  3328.   line_number = real_line_number;
  3329. }
  3330.  
  3331. /* The actual commands themselves. */
  3332.  
  3333. /* Commands which insert themselves. */
  3334. insert_self ()
  3335. {
  3336.   add_word (command);
  3337. }
  3338.  
  3339. /* Force a line break in the output. */
  3340. cm_asterisk ()
  3341. {
  3342.   close_single_paragraph ();
  3343. #if !defined (ASTERISK_NEW_PARAGRAPH)
  3344.   cm_noindent ();
  3345. #endif /* ASTERISK_NEW_PARAGRAPH */  
  3346. }
  3347.  
  3348. /* Insert ellipsis. */
  3349. cm_dots (arg)
  3350.      int arg;
  3351. {
  3352.   if (arg == START)
  3353.     add_word ("...");
  3354. }
  3355.  
  3356. cm_bullet (arg)
  3357.      int arg;
  3358. {
  3359.   if (arg == START)
  3360.     add_char ('*');
  3361. }
  3362.  
  3363. cm_minus (arg)
  3364.      int arg;
  3365. {
  3366.   if (arg == START)
  3367.     add_char ('-');
  3368. }
  3369.  
  3370. /* Insert "TeX". */
  3371. cm_TeX (arg)
  3372.      int arg;
  3373. {
  3374.   if (arg == START)
  3375.     add_word ("TeX");
  3376. }
  3377.  
  3378. cm_copyright (arg)
  3379.      int arg;
  3380. {
  3381.   if (arg == START)
  3382.     add_word ("(C)");
  3383. }
  3384.  
  3385. cm_today (arg)
  3386.      int arg;
  3387. {
  3388.   static char * months [12] =
  3389.     { "January", "February", "March", "April", "May", "June", "July",
  3390.     "August", "September", "October", "November", "December" };
  3391.   if (arg == START)
  3392.     {
  3393.       long timer = (time (0));
  3394.       struct tm *ts = (localtime (&timer));
  3395.       add_word_args
  3396.     ("%d %s %d",
  3397.      (ts -> tm_mday),
  3398.      (months [ts -> tm_mon]),
  3399.      ((ts -> tm_year) + 1900));
  3400.     }
  3401. }
  3402.  
  3403. cm_code (arg)
  3404.      int arg;
  3405. {
  3406.   extern int printing_index;
  3407.  
  3408.   if (printing_index)
  3409.     return;
  3410.  
  3411.   if (arg == START)
  3412.     {
  3413.       in_fixed_width_font++;
  3414.       add_char ('`');
  3415.     }
  3416.   else
  3417.     {
  3418.       add_word ("'");
  3419.       in_fixed_width_font--;
  3420.     }
  3421. }
  3422.  
  3423. cm_samp (arg)
  3424.      int arg;
  3425. {
  3426.   cm_code (arg);
  3427. }
  3428.  
  3429. cm_file (arg)
  3430.      int arg;
  3431. {
  3432.   cm_code (arg);
  3433. }
  3434.  
  3435. cm_kbd (arg)
  3436.      int arg;
  3437. {
  3438.   cm_code (arg);
  3439. }
  3440.  
  3441. cm_key (arg)
  3442.      int arg;
  3443. {
  3444. }
  3445.  
  3446. /* Convert the character at position into CTL. */
  3447. cm_ctrl (arg, position)
  3448.      int arg, position;
  3449. {
  3450.   if (arg == END)
  3451.     output_paragraph[position - 1] = CTL (output_paragraph[position]);
  3452. }
  3453.  
  3454. /* Small Caps in makeinfo just does all caps. */
  3455. cm_sc (arg, start_pos, end_pos)
  3456.      int arg, start_pos, end_pos;
  3457. {
  3458.   if (arg == END)
  3459.     {
  3460.       while (start_pos < end_pos)
  3461.     {
  3462.       output_paragraph[start_pos] =
  3463.         coerce_to_upper (output_paragraph[start_pos]);
  3464.       start_pos++;
  3465.     }
  3466.     }
  3467. }
  3468.  
  3469. /* @var in makeinfo just uppercases the text. */
  3470. cm_var (arg, start_pos, end_pos)
  3471.      int arg, start_pos, end_pos;
  3472. {
  3473.   if (arg == END)
  3474.     {
  3475.       while (start_pos < end_pos)
  3476.     {
  3477.       output_paragraph[start_pos] =
  3478.         coerce_to_upper (output_paragraph[start_pos]);
  3479.       start_pos++;
  3480.     }
  3481.     }
  3482. }
  3483.  
  3484. cm_dfn (arg, position)
  3485.      int arg, position;
  3486. {
  3487.   add_char ('"');
  3488. }
  3489.  
  3490. cm_emph (arg)
  3491.      int arg;
  3492. {
  3493.   add_char ('*');
  3494. }
  3495.  
  3496. cm_strong (arg, position)
  3497.      int arg, position;
  3498. {
  3499.   cm_emph (arg);
  3500. }
  3501.  
  3502. cm_cite (arg, position)
  3503.      int arg, position;
  3504. {
  3505.   if (arg == START)
  3506.     add_word ("`");
  3507.   else
  3508.     add_word ("'");
  3509. }
  3510.  
  3511. /* Current text is italicized. */
  3512. cm_italic (arg, start, end)
  3513.      int arg, start, end;
  3514. {
  3515. }
  3516.  
  3517. /* Current text is highlighted. */
  3518. cm_bold (arg, start, end)
  3519.      int arg, start, end;
  3520. {
  3521.   cm_italic (arg);
  3522. }
  3523.  
  3524. /* Current text is in roman font. */
  3525. cm_roman (arg, start, end)
  3526.      int arg, start, end;
  3527. {
  3528. }
  3529.  
  3530. /* Current text is in roman font. */
  3531. cm_titlefont (arg, start, end)
  3532.      int arg, start, end;
  3533. {
  3534. }
  3535.  
  3536. /* Italicize titles. */
  3537. cm_title (arg, start, end)
  3538.      int arg, start, end;
  3539. {
  3540.   cm_italic (arg);
  3541. }
  3542.  
  3543. /* @refill is a NOP. */
  3544. cm_refill ()
  3545. {
  3546. }
  3547.  
  3548. /* Prevent the argument from being split across two lines. */
  3549. cm_w (arg, start, end)
  3550.      int arg, start, end;
  3551. {
  3552.   if (arg == START)
  3553.     non_splitting_words++;
  3554.   else
  3555.     non_splitting_words--;
  3556. }
  3557.  
  3558.  
  3559. /* Explain that this command is obsolete, thus the user shouldn't
  3560.    do anything with it. */
  3561. cm_obsolete (arg, start, end)
  3562.      int arg, start, end;
  3563. {
  3564.   if (arg == START)
  3565.     warning ("The command `@%s' is obsolete", command);
  3566. }
  3567.  
  3568. /* Insert the text following input_text_offset up to the end of the line
  3569.    in a new, separate paragraph.  Directly underneath it, insert a
  3570.    line of WITH_CHAR, the same length of the inserted text. */
  3571. insert_and_underscore (with_char)
  3572.      int with_char;
  3573. {
  3574.   int len, i, old_no_indent;
  3575.   int starting_pos, ending_pos;
  3576.   char *temp;
  3577.  
  3578.   close_paragraph ();
  3579.   filling_enabled =  indented_fill = 0;
  3580.   old_no_indent = no_indent;
  3581.   no_indent = 1;
  3582.   get_rest_of_line (&temp);
  3583.  
  3584.   starting_pos = output_position + output_paragraph_offset;
  3585.   execute_string ("%s\n", temp);
  3586.   ending_pos = output_position + output_paragraph_offset;
  3587.   free (temp);
  3588.  
  3589.   len = (ending_pos - starting_pos) - 1;
  3590.   for (i = 0; i < len; i++)
  3591.     add_char (with_char);
  3592.   insert ('\n');
  3593.   close_paragraph ();
  3594.   filling_enabled = 1;
  3595.   no_indent = old_no_indent;
  3596. }
  3597.  
  3598. /* Here is a structure which associates sectioning commands with
  3599.    an integer, hopefully to reflect the `depth' of the current
  3600.    section. */
  3601. struct {
  3602.   char *name;
  3603.   int level;
  3604. } section_alist[] = {
  3605.   { "unnumberedsubsubsec", 5 },
  3606.   { "unnumberedsubsec", 4 },
  3607.   { "unnumberedsec", 3 },
  3608.   { "unnumbered", 2 },
  3609.   { "appendixsubsubsec", 5 },
  3610.   { "appendixsubsec", 4 },
  3611.   { "appendixsec", 3 },
  3612.   { "appendixsection", 3 },
  3613.   { "appendix", 2 },
  3614.   { "subsubsec", 5 },
  3615.   { "subsubsection", 5 },
  3616.   { "subsection", 4 },
  3617.   { "section", 3 },
  3618.   { "chapter", 2 },
  3619.   { "top", 1 },
  3620.  
  3621.   { (char *)NULL, 0 }
  3622. };
  3623.  
  3624. /* Amount to offset the name of sectioning commands to levels by. */
  3625. int section_alist_offset = 0;
  3626.  
  3627. /* Shift the meaning of @section to @chapter. */
  3628. cm_raisesections ()
  3629. {
  3630.   discard_until ("\n");
  3631.   section_alist_offset--;
  3632. }
  3633.  
  3634. /* Shift the meaning of @chapter to @section. */
  3635. cm_lowersections ()
  3636. {
  3637.   discard_until ("\n");
  3638.   section_alist_offset++;
  3639. }
  3640.  
  3641. /* Return an integer which identifies the type section present in TEXT. */
  3642. int
  3643. what_section (text)
  3644.      char *text;
  3645. {
  3646.   register int i, j;
  3647.   char *t;
  3648.  
  3649.  find_section_command:
  3650.   for (j = 0; text[j] && cr_or_whitespace (text[j]); j++);
  3651.   if (text[j] != '@')
  3652.     return (-1);
  3653.  
  3654.   text = text + j + 1;
  3655.  
  3656.   /* We skip @c, @comment, and @?index commands. */
  3657.   if ((strncmp (text, "comment", strlen ("comment")) == 0) ||
  3658.       (text[0] == 'c' && cr_or_whitespace (text[1])) ||
  3659.       (strcmp (text + 1, "index") == 0))
  3660.     {
  3661.       while (*text++ != '\n');
  3662.       goto find_section_command;
  3663.     }
  3664.  
  3665.   /* Handle italicized sectioning commands. */
  3666.   if (*text == 'i')
  3667.     text++;
  3668.  
  3669.   for (j = 0; text[j] && !cr_or_whitespace (text[j]); j++);
  3670.  
  3671.   for (i = 0; t = section_alist[i].name; i++)
  3672.     {
  3673.       if (j == strlen (t) && strncmp (t, text, j) == 0)
  3674.     {
  3675.       int return_val;
  3676.  
  3677.       return_val = (section_alist[i].level + section_alist_offset);
  3678.  
  3679.       if (return_val < 0)
  3680.         return_val = 0;
  3681.       else if (return_val > 5)
  3682.         return_val = 5;
  3683.       return (return_val);
  3684.     }
  3685.     }
  3686.   return (-1);
  3687. }
  3688.  
  3689. /* Treat this just like @unnumbered.  The only difference is
  3690.    in node defaulting. */
  3691. cm_top ()
  3692. {
  3693.   static int top_encountered = 0;
  3694.   cm_unnumbered ();
  3695.  
  3696.   /* It is an error to have more than one @top. */
  3697.   if (top_encountered)
  3698.     {
  3699.       TAG_ENTRY *tag = tag_table;
  3700.  
  3701.       line_error ("There already is a node having @top as a section");
  3702.  
  3703.       while (tag != (TAG_ENTRY *)NULL)
  3704.     {
  3705.       if ((tag->flags & IS_TOP))
  3706.         {
  3707.           int old_line_number = line_number;
  3708.           char *old_input_filename = input_filename;
  3709.  
  3710.           line_number = tag->line_no;
  3711.           input_filename = tag->filename;
  3712.           line_error ("Here is the @top node.");
  3713.           input_filename = old_input_filename;
  3714.           line_number = old_line_number;
  3715.           return;
  3716.         }
  3717.       tag = tag->next_ent;
  3718.     }
  3719.     }
  3720.   else
  3721.     {
  3722.       top_encountered = 1;
  3723.  
  3724.       /* The most recently defined node is the top node. */
  3725.       if (tag_table)
  3726.     tag_table->flags |= IS_TOP;
  3727.  
  3728.       /* Now set the logical hierarchical level of the Top node. */
  3729.       {
  3730.     int orig_offset = input_text_offset;
  3731.  
  3732.     input_text_offset = search_forward ("\n@node", orig_offset);
  3733.  
  3734.     if (input_text_offset > 0)
  3735.       {
  3736.         int this_section;
  3737.  
  3738.         /* Move to the end of this line, and find out what the
  3739.            sectioning command is here. */
  3740.         while (input_text[input_text_offset] != '\n')
  3741.           input_text_offset++;
  3742.  
  3743.         if (input_text_offset < size_of_input_text)
  3744.           input_text_offset++;
  3745.  
  3746.         this_section = what_section (input_text + input_text_offset);
  3747.  
  3748.         /* If we found a sectioning command, then give the top section
  3749.            a level of this section - 1. */
  3750.         if (this_section != -1)
  3751.           {
  3752.         register int i;
  3753.  
  3754.         for (i = 0; section_alist[i].name; i++)
  3755.           if (strcmp (section_alist[i].name, "Top") == 0)
  3756.             {
  3757.               section_alist[i].level = this_section - 1;
  3758.               break;
  3759.             }
  3760.           }
  3761.       }
  3762.     input_text_offset = orig_offset;
  3763.       }
  3764.     }
  3765. }
  3766.  
  3767. /* Organized by level commands.  That is, "*" == chapter, "=" == section. */
  3768. char *scoring_characters = "*=-.";
  3769.  
  3770. void
  3771. sectioning_underscore (command)
  3772.      char *command;
  3773. {
  3774.   char character;
  3775.   int level;
  3776.  
  3777.   level = what_section (command);
  3778.   level -= 2;
  3779.  
  3780.   if (level < 0)
  3781.     level = 0;
  3782.  
  3783.   character = scoring_characters[level];
  3784.  
  3785.   insert_and_underscore (character);
  3786. }
  3787.  
  3788. /* The remainder of the text on this line is a chapter heading. */
  3789. cm_chapter ()
  3790. {
  3791.   sectioning_underscore ("@chapter");
  3792. }
  3793.  
  3794. /* The remainder of the text on this line is a section heading. */
  3795. cm_section ()
  3796. {
  3797.   sectioning_underscore ("@section");
  3798. }
  3799.  
  3800. /* The remainder of the text on this line is a subsection heading. */
  3801. cm_subsection ()
  3802. {
  3803.   sectioning_underscore ("@subsection");
  3804. }
  3805.  
  3806. /* The remainder of the text on this line is a subsubsection heading. */
  3807. cm_subsubsection ()
  3808. {
  3809.   sectioning_underscore ("@subsubsection");
  3810. }
  3811.  
  3812. /* The remainder of the text on this line is an unnumbered heading. */
  3813. cm_unnumbered ()
  3814. {
  3815.   cm_chapter ();
  3816. }
  3817.  
  3818. /* The remainder of the text on this line is an unnumbered section heading. */
  3819. cm_unnumberedsec ()
  3820. {
  3821.   cm_section ();
  3822. }
  3823.  
  3824. /* The remainder of the text on this line is an unnumbered
  3825.    subsection heading. */
  3826. cm_unnumberedsubsec ()
  3827. {
  3828.   cm_subsection ();
  3829. }
  3830.  
  3831. /* The remainder of the text on this line is an unnumbered
  3832.    subsubsection heading. */
  3833. cm_unnumberedsubsubsec ()
  3834. {
  3835.   cm_subsubsection ();
  3836. }
  3837.  
  3838. /* The remainder of the text on this line is an appendix heading. */
  3839. cm_appendix ()
  3840. {
  3841.   cm_chapter ();
  3842. }
  3843.  
  3844. /* The remainder of the text on this line is an appendix section heading. */
  3845. cm_appendixsec ()
  3846. {
  3847.   cm_section ();
  3848. }
  3849.  
  3850. /* The remainder of the text on this line is an appendix subsection heading. */
  3851. cm_appendixsubsec ()
  3852. {
  3853.   cm_subsection ();
  3854. }
  3855.  
  3856. /* The remainder of the text on this line is an appendix
  3857.    subsubsection heading. */
  3858. cm_appendixsubsubsec ()
  3859. {
  3860.   cm_subsubsection ();
  3861. }
  3862.  
  3863. /* Compatibility functions substitute for chapter, section, etc. */
  3864. cm_majorheading ()
  3865. {
  3866.   cm_chapheading ();
  3867. }
  3868.  
  3869. cm_chapheading ()
  3870. {
  3871.   cm_chapter ();
  3872. }
  3873.  
  3874. cm_heading ()
  3875. {
  3876.   cm_section ();
  3877. }
  3878.  
  3879. cm_subheading ()
  3880. {
  3881.   cm_subsection ();
  3882. }
  3883.  
  3884. cm_subsubheading ()
  3885. {
  3886.   cm_subsubsection ();
  3887. }
  3888.  
  3889.  
  3890. /* **************************************************************** */
  3891. /*                                    */
  3892. /*           Adding nodes, and making tags            */
  3893. /*                                    */
  3894. /* **************************************************************** */
  3895.  
  3896. /* Start a new tag table. */
  3897. init_tag_table ()
  3898. {
  3899.   while (tag_table != (TAG_ENTRY *) NULL)
  3900.     {
  3901.       TAG_ENTRY *temp = tag_table;
  3902.       free (temp->node);
  3903.       free (temp->prev);
  3904.       free (temp->next);
  3905.       free (temp->up);
  3906.       tag_table = tag_table->next_ent;
  3907.       free (temp);
  3908.     }
  3909. }
  3910.  
  3911. write_tag_table ()
  3912. {
  3913.   return (write_tag_table_internal (0));    /* Not indirect. */
  3914. }
  3915.  
  3916. write_tag_table_indirect ()
  3917. {
  3918.   return (write_tag_table_internal (1));
  3919. }
  3920.  
  3921. /* Write out the contents of the existing tag table.
  3922.    INDIRECT_P says how to format the output. */
  3923. write_tag_table_internal (indirect_p)
  3924.      int indirect_p;
  3925. {
  3926.   TAG_ENTRY *node = tag_table;
  3927.   int old_indent = no_indent;
  3928.  
  3929.   no_indent = 1;
  3930.   filling_enabled = 0;
  3931.   must_start_paragraph = 0;
  3932.   close_paragraph ();
  3933.  
  3934.   if (!indirect_p)
  3935.     {
  3936.       no_indent = 1;
  3937.       insert ('\n');
  3938.     }
  3939.  
  3940.   add_word_args ("\037\nTag Table:\n%s", indirect_p ? "(Indirect)\n" : "");
  3941.  
  3942.   while (node != (TAG_ENTRY *) NULL)
  3943.     {
  3944.       add_word_args ("Node: %s\177%d\n", node->node, node->position);
  3945.       node = node->next_ent;
  3946.     }
  3947.  
  3948.   add_word ("\037\nEnd Tag Table\n");
  3949.   flush_output ();
  3950.   no_indent = old_indent;
  3951. }
  3952.  
  3953. char *
  3954. get_node_token ()
  3955. {
  3956.   char *string;
  3957.  
  3958.   get_until_in_line (",", &string);
  3959.  
  3960.   if (curchar () == ',')
  3961.     input_text_offset++;
  3962.  
  3963.   canon_white (string);
  3964.  
  3965.   /* Allow things like @@nodename. */
  3966.   normalize_node_name (string);
  3967.  
  3968.   return (string);
  3969. }
  3970.  
  3971. /* Given a node name in STRING, remove double @ signs, replacing them
  3972.    with just one.  Convert "top" and friends into "Top". */
  3973. normalize_node_name (string)
  3974.      char *string;
  3975. {
  3976.   register int i, l = strlen (string);
  3977.  
  3978.   for (i = 0; i < l; i++)
  3979.     {
  3980.       if (string[i] == '@' && string[i + 1] == '@')
  3981.     {
  3982.       strncpy (string + i, string + i + 1, l - i);
  3983.       l--;
  3984.     }
  3985.     }
  3986.   if (stricmp (string, "Top") == 0)
  3987.     strcpy (string, "Top");
  3988. }
  3989.  
  3990. /* Look up NAME in the tag table, and return the associated
  3991.    tag_entry.  If the node is not in the table return NULL. */
  3992. TAG_ENTRY *
  3993. find_node (name)
  3994.      char *name;
  3995. {
  3996.   TAG_ENTRY *tag = tag_table;
  3997.  
  3998.   while (tag != (TAG_ENTRY *) NULL)
  3999.     {
  4000.       if (strcmp (tag->node, name) == 0)
  4001.     return (tag);
  4002.       tag = tag->next_ent;
  4003.     }
  4004.   return ((TAG_ENTRY *) NULL);
  4005. }
  4006.  
  4007. /* Remember NODE and associates. */
  4008. remember_node (node, prev, next, up, position, line_no, no_warn)
  4009.      char *node, *prev, *next, *up;
  4010.      int position, line_no, no_warn;
  4011. {
  4012.   /* Check for existence of this tag already. */
  4013.   if (validating)
  4014.     {
  4015.       register TAG_ENTRY *tag = find_node (node);
  4016.       if (tag)
  4017.     {
  4018.       line_error ("Node `%s' multiply defined (%d is first definition)",
  4019.               node, tag->line_no);
  4020.       return;
  4021.     }
  4022.     }
  4023.  
  4024.   /* First, make this the current node. */
  4025.   current_node = node;
  4026.  
  4027.   /* Now add it to the list. */
  4028.   {
  4029.     TAG_ENTRY *new = (TAG_ENTRY *) xmalloc (sizeof (TAG_ENTRY));
  4030.     new->node = node;
  4031.     new->prev = prev;
  4032.     new->next = next;
  4033.     new->up = up;
  4034.     new->position = position;
  4035.     new->line_no = line_no;
  4036.     new->filename = node_filename;
  4037.     new->touched = 0;        /* not yet referenced. */
  4038.     new->flags = 0;
  4039.     if (no_warn)
  4040.       new->flags |= NO_WARN;
  4041.     new->next_ent = tag_table;
  4042.     tag_table = new;
  4043.   }
  4044. }
  4045.  
  4046. /* The order is: nodename, nextnode, prevnode, upnode.
  4047.    If all of the NEXT, PREV, and UP fields are empty, they are defaulted.
  4048.    You must follow a node command which has those fields defaulted
  4049.    with a sectioning command (e.g. @chapter) giving the "level" of that node.
  4050.    It is an error not to do so.
  4051.    The defaults come from the menu in this nodes parent. */
  4052. cm_node ()
  4053. {
  4054.   char *node, *prev, *next, *up;
  4055.   int new_node_pos, defaulting, this_section, no_warn = 0;
  4056.   extern int already_outputting_pending_notes;
  4057.  
  4058.   if (strcmp (command, "nwnode") == 0)
  4059.     no_warn = 1;
  4060.  
  4061.   /* Get rid of unmatched brace arguments from previous commands. */
  4062.   discard_braces ();
  4063.  
  4064.   /* There also might be insertions left lying around that haven't been
  4065.      ended yet.  Do that also. */
  4066.   discard_insertions ();
  4067.  
  4068.   if (!already_outputting_pending_notes)
  4069.     {
  4070.       close_paragraph ();
  4071.       output_pending_notes ();
  4072.       free_pending_notes ();
  4073.     }
  4074.  
  4075.   filling_enabled = indented_fill = 0;
  4076.   new_node_pos = output_position;
  4077.   current_footnote_number = 1;
  4078.  
  4079.   node = get_node_token ();
  4080.   next = get_node_token ();
  4081.   prev = get_node_token ();
  4082.   up = get_node_token ();
  4083.  
  4084.   no_indent = 1;
  4085.   if (!no_headers)
  4086.     add_word_args ("\037\nFile: %s,  Node: %s", pretty_output_filename, node);
  4087.  
  4088.   /* Check for defaulting of this node's next, prev, and up fields. */
  4089.   defaulting = ((strlen (next) == 0) &&
  4090.         (strlen (prev) == 0) &&
  4091.         (strlen (up) == 0));
  4092.  
  4093.   this_section = what_section (input_text + input_text_offset);
  4094.  
  4095.   /* If we are defaulting, then look at the immediately following
  4096.      sectioning command (error if none) to determine the node's
  4097.      level.  Find the node that contains the menu mentioning this node
  4098.      that is one level up (error if not found).  That node is the "Up"
  4099.      of this node.  Default the "Next" and "Prev" from the menu. */
  4100.   if (defaulting)
  4101.     {
  4102.       NODE_REF *last_ref = (NODE_REF *)NULL;
  4103.       NODE_REF *ref = node_references;
  4104.  
  4105.       if (this_section < 0)
  4106.     {
  4107.       char *polite_section_name = "top";
  4108.       int i;
  4109.  
  4110.       for (i = 0; section_alist[i].name; i++)
  4111.         if (section_alist[i].level == current_section + 1)
  4112.           {
  4113.         polite_section_name = section_alist[i].name;
  4114.         break;
  4115.           }
  4116.  
  4117.       line_error
  4118.         ("Node `%s' requires a sectioning command (e.g. @%s)",
  4119.          node, polite_section_name);
  4120.     }
  4121.       else
  4122.     {
  4123.       if (stricmp (node, "Top") == 0)
  4124.         {
  4125.           /* Default the NEXT pointer to be the first menu item in
  4126.          this node, if there is a menu in this node. */
  4127.           {
  4128.         int orig_offset, orig_size;
  4129.         char *glean_node_from_menu ();
  4130.  
  4131.         orig_offset = input_text_offset;
  4132.         orig_size = search_forward ("\n@node ", orig_offset);
  4133.  
  4134.         if (orig_size < 0)
  4135.           orig_size = size_of_input_text;
  4136.  
  4137.         input_text_offset = search_forward ("\n@menu", orig_offset);
  4138.         if (input_text_offset > -1)
  4139.           {
  4140.             char *nodename_from_menu = (char *)NULL;
  4141.  
  4142.             input_text_offset =
  4143.               search_forward ("\n* ", input_text_offset);
  4144.  
  4145.             if (input_text_offset != -1)
  4146.               nodename_from_menu = glean_node_from_menu (0);
  4147.  
  4148.             if (nodename_from_menu)
  4149.               {
  4150.             free (next);
  4151.             next = nodename_from_menu;
  4152.             prev = savestring ("(DIR)");
  4153.             up = savestring ("(DIR)");
  4154.               }
  4155.           }
  4156.         input_text_offset = orig_offset;
  4157.           }
  4158.         }
  4159.  
  4160.       while (ref)
  4161.         {
  4162.           if (ref->section == (this_section - 1) &&
  4163.           ref->type == menu_reference &&
  4164.           strcmp (ref->node, node) == 0)
  4165.         {
  4166.           char *containing_node = ref->containing_node;
  4167.  
  4168.           free (up);
  4169.           up = savestring (containing_node);
  4170.  
  4171.           if (last_ref &&
  4172.               last_ref->type == menu_reference &&
  4173.               (strcmp (last_ref->containing_node,
  4174.                    containing_node) == 0))
  4175.             {
  4176.               free (next);
  4177.               next = savestring (last_ref->node);
  4178.             }
  4179.  
  4180.           while ((ref->section == this_section - 1) &&
  4181.              (ref->next) &&
  4182.              (ref->next->type != menu_reference))
  4183.             ref = ref->next;
  4184.  
  4185.           if (ref->next && ref->type == menu_reference &&
  4186.               (strcmp (ref->next->containing_node,
  4187.                    containing_node) == 0))
  4188.             {
  4189.               free (prev);
  4190.               prev = savestring (ref->next->node);
  4191.             }
  4192.           else if (!ref->next &&
  4193.                stricmp (ref->containing_node, "Top") == 0)
  4194.             {
  4195.               free (prev);
  4196.               prev = savestring (ref->containing_node);
  4197.             }
  4198.           break;
  4199.         }
  4200.           last_ref = ref;
  4201.           ref = ref->next;
  4202.         }
  4203.     }
  4204.     }
  4205.  
  4206.   if (!no_headers)
  4207.     {
  4208.       if (*next)
  4209.     add_word_args (",  Next: %s", next);
  4210.  
  4211.       if (*prev)
  4212.     add_word_args (",  Prev: %s", prev);
  4213.  
  4214.       if (*up)
  4215.     add_word_args (",  Up: %s", up);
  4216.     }
  4217.  
  4218.   close_paragraph ();
  4219.   no_indent = 0;
  4220.  
  4221.   if (!*node)
  4222.     {
  4223.       line_error ("No node name specified for `@%s' command", command);
  4224.       free (node);
  4225.       free (next);
  4226.       free (prev);
  4227.       free (up);
  4228.     }
  4229.   else
  4230.     {
  4231.       if (!*next) { free (next); next = (char *)NULL; }
  4232.       if (!*prev) { free (prev); prev = (char *)NULL; }
  4233.       if (!*up) { free (up); up = (char *)NULL; }
  4234.       remember_node (node, prev, next, up, new_node_pos, line_number, no_warn);
  4235.     }
  4236.  
  4237.   /* Change the section only if there was a sectioning command. */
  4238.   if (this_section >= 0)
  4239.     current_section = this_section;
  4240.  
  4241.   filling_enabled = 1;
  4242. }
  4243.  
  4244. /* Validation of an info file.
  4245.    Scan through the list of tag entrys touching the Prev, Next, and Up
  4246.    elements of each.  It is an error not to be able to touch one of them,
  4247.    except in the case of external node references, such as "(DIR)".
  4248.  
  4249.    If the Prev is different from the Up,
  4250.    then the Prev node must have a Next pointing at this node.
  4251.  
  4252.    Every node except Top must have an Up.
  4253.    The Up node must contain some sort of reference, other than a Next,
  4254.    to this node.
  4255.  
  4256.    If the Next is different from the Next of the Up,
  4257.    then the Next node must have a Prev pointing at this node. */
  4258. validate_file (filename, tag_table)
  4259.      char *filename;
  4260.      TAG_ENTRY *tag_table;
  4261. {
  4262.   char *old_input_filename = input_filename;
  4263.   TAG_ENTRY *tags = tag_table;
  4264.  
  4265.   while (tags != (TAG_ENTRY *) NULL)
  4266.     {
  4267.       register TAG_ENTRY *temp_tag;
  4268.  
  4269.       input_filename = tags->filename;
  4270.       line_number = tags->line_no;
  4271.  
  4272.       /* If this is a "no warn" node, don't validate it in any way. */
  4273.       if (tags->flags & NO_WARN)
  4274.     {
  4275.       tags = tags->next_ent;
  4276.       continue;
  4277.     }
  4278.     
  4279.       /* If this node has a Next, then make sure that the Next exists. */
  4280.       if (tags->next)
  4281.     {
  4282.       validate (tags->next, tags->line_no, "Next");
  4283.  
  4284.       /* If the Next node exists, and there is no Up, then make
  4285.          sure that the Prev of the Next points back. */
  4286.       if (temp_tag = find_node (tags->next))
  4287.         {
  4288.           char *prev;
  4289.  
  4290.           if (temp_tag->flags & NO_WARN)
  4291.         {
  4292.           /* Do nothing if we aren't supposed to issue warnings
  4293.              about this node. */
  4294.         }
  4295.           else
  4296.         {
  4297.           prev = temp_tag->prev;
  4298.           if (!prev || (strcmp (prev, tags->node) != 0))
  4299.             {
  4300.               line_error ("Node `%s''s Next field not pointed back to",
  4301.                   tags->node);
  4302.               line_number = temp_tag->line_no;
  4303.               input_filename = temp_tag->filename;
  4304.               line_error
  4305.             ("This node (`%s') is the one with the bad `Prev'",
  4306.              temp_tag->node);
  4307.               input_filename = tags->filename;
  4308.               line_number = tags->line_no;
  4309.               temp_tag->flags |= PREV_ERROR;
  4310.             }
  4311.         }
  4312.         }
  4313.     }
  4314.  
  4315.       /* Validate the Prev field if there is one, and we haven't already
  4316.      complained about it in some way.  You don't have to have a Prev
  4317.      field at this stage. */
  4318.       if (!(tags->flags & PREV_ERROR) && tags->prev)
  4319.     {
  4320.       int valid = validate (tags->prev, tags->line_no, "Prev");
  4321.  
  4322.       if (!valid)
  4323.         tags->flags |= PREV_ERROR;
  4324.       else
  4325.         {
  4326.           /* If the Prev field is not the same as the Up field,
  4327.          then the node pointed to by the Prev field must have
  4328.          a Next field which points to this node. */
  4329.           if (tags->up && (strcmp (tags->prev, tags->up) != 0))
  4330.         {
  4331.           temp_tag = find_node (tags->prev);
  4332.  
  4333.           /* If we aren't supposed to issue warnings about the
  4334.              target node, do nothing. */
  4335.           if (!temp_tag || (temp_tag->flags & NO_WARN))
  4336.             {
  4337.               /* Do nothing. */
  4338.             }
  4339.           else
  4340.             {
  4341.               if (!temp_tag->next ||
  4342.               (strcmp (temp_tag->next, tags->node) != 0))
  4343.             {
  4344.               line_error
  4345.                 ("Node `%s''s Prev field not pointed back to",
  4346.                  tags->node);
  4347.               line_number = temp_tag->line_no;
  4348.               input_filename = temp_tag->filename;
  4349.               line_error
  4350.                 ("This node (`%s') is the one with the bad `Next'",
  4351.                  temp_tag->node);
  4352.               input_filename = tags->filename;
  4353.               line_number = tags->line_no;
  4354.               temp_tag->flags |= NEXT_ERROR;
  4355.             }
  4356.             }
  4357.         }
  4358.         }
  4359.     }
  4360.  
  4361.       if (!tags->up && (stricmp (tags->node, "Top") != 0))
  4362.     line_error ("Node `%s' is missing an \"Up\" field", tags->node);
  4363.       else if (tags->up)
  4364.     {
  4365.       int valid = validate (tags->up, tags->line_no, "Up");
  4366.  
  4367.       /* If node X has Up: Y, then warn if Y fails to have a menu item
  4368.          or note pointing at X, if Y isn't of the form "(Y)". */
  4369.       if (valid && *tags->up != '(')
  4370.         {
  4371.           NODE_REF *nref, *tref, *list;
  4372.           NODE_REF *find_node_reference ();
  4373.  
  4374.           tref = (NODE_REF *) NULL;
  4375.           list = node_references;
  4376.  
  4377.           for (;;)
  4378.         {
  4379.           if (!(nref = find_node_reference (tags->node, list)))
  4380.             break;
  4381.  
  4382.           if (strcmp (nref->containing_node, tags->up) == 0)
  4383.             {
  4384.               if (nref->type != menu_reference)
  4385.             {
  4386.               tref = nref;
  4387.               list = nref->next;
  4388.             }
  4389.               else
  4390.             break;
  4391.             }
  4392.           list = nref->next;
  4393.         }
  4394.  
  4395.           if (!nref)
  4396.         {
  4397.           temp_tag = find_node (tags->up);
  4398.           line_number = temp_tag->line_no;
  4399.           filename = temp_tag->filename;
  4400.           if (!tref)
  4401.             line_error (
  4402. "`%s' has an Up field of `%s', but `%s' has no menu item for `%s'",
  4403.                 tags->node, tags->up, tags->up, tags->node);
  4404.           line_number = tags->line_no;
  4405.           filename = tags->filename;
  4406.         }
  4407.         }
  4408.     }
  4409.       tags = tags->next_ent;
  4410.     }
  4411.  
  4412.   validate_other_references (node_references);
  4413.   /* We have told the user about the references which didn't exist.
  4414.      Now tell him about the nodes which aren't referenced. */
  4415.  
  4416.   tags = tag_table;
  4417.   while (tags != (TAG_ENTRY *) NULL)
  4418.     {
  4419.       /* If this node is a "no warn" node, do nothing. */
  4420.       if (tags->flags & NO_WARN)
  4421.     {
  4422.       tags = tags->next_ent;
  4423.       continue;
  4424.     }
  4425.  
  4426.       /* Special hack.  If the node in question appears to have
  4427.          been referenced more than REFERENCE_WARNING_LIMIT times,
  4428.          give a warning. */
  4429.       if (tags->touched > reference_warning_limit)
  4430.     {
  4431.       input_filename = tags->filename;
  4432.       line_number = tags->line_no;
  4433.       warning ("Node `%s' has been referenced %d times",
  4434.            tags->node, tags->touched);
  4435.     }
  4436.  
  4437.       if (tags->touched == 0)
  4438.     {
  4439.       input_filename = tags->filename;
  4440.       line_number = tags->line_no;
  4441.  
  4442.       /* Notice that the node "Top" is special, and doesn't have to
  4443.          be referenced. */
  4444.       if (stricmp (tags->node, "Top") != 0)
  4445.         warning ("Unreferenced node `%s'", tags->node);
  4446.     }
  4447.       tags = tags->next_ent;
  4448.     }
  4449.   input_filename = old_input_filename;
  4450. }
  4451.  
  4452. /* Return 1 if tag correctly validated, or 0 if not. */
  4453. validate (tag, line, label)
  4454.      char *tag;
  4455.      int line;
  4456.      char *label;
  4457. {
  4458.   TAG_ENTRY *result;
  4459.  
  4460.   /* If there isn't a tag to verify, or if the tag is in another file,
  4461.      then it must be okay. */
  4462.   if (!tag || !*tag || *tag == '(')
  4463.     return (1);
  4464.  
  4465.   /* Otherwise, the tag must exist. */
  4466.   result = find_node (tag);
  4467.  
  4468.   if (!result)
  4469.     {
  4470.       line_number = line;
  4471.       line_error (
  4472. "Validation error.  `%s' field points to node `%s', which doesn't exist",
  4473.           label, tag);
  4474.       return (0);
  4475.     }
  4476.   result->touched++;
  4477.   return (1);
  4478. }
  4479.  
  4480. /* Split large output files into a series of smaller files.  Each file
  4481.    is pointed to in the tag table, which then gets written out as the
  4482.    original file.  The new files have the same name as the original file
  4483.    with a "-num" attached.  SIZE is the largest number of bytes to allow
  4484.    in any single split file. */
  4485. split_file (filename, size)
  4486.      char *filename;
  4487.      int size;
  4488. {
  4489.   char *root_filename, *root_pathname;
  4490.   char *the_file, *filename_part ();
  4491.   struct stat fileinfo;
  4492.   char *the_header;
  4493.   int header_size;
  4494.  
  4495.   /* Can only do this to files with tag tables. */
  4496.   if (!tag_table)
  4497.     return;
  4498.  
  4499.   if (size == 0)
  4500.     size = DEFAULT_SPLIT_SIZE;
  4501.  
  4502.   if ((stat (filename, &fileinfo) != 0) ||
  4503.       (fileinfo.st_size < SPLIT_SIZE_THRESHOLD))
  4504.     return;
  4505.  
  4506.   the_file = find_and_load (filename);
  4507.   if (!the_file)
  4508.     return;
  4509.  
  4510.   root_filename = filename_part (filename);
  4511.   root_pathname = pathname_part (filename);
  4512.  
  4513.   if (!root_pathname)
  4514.     root_pathname = savestring ("");
  4515.  
  4516.   /* Start splitting the file.  Walk along the tag table
  4517.      outputting sections of the file.  When we have written
  4518.      all of the nodes in the tag table, make the top-level
  4519.      pointer file, which contains indirect pointers and
  4520.      tags for the nodes. */
  4521.   {
  4522.     int which_file = 1;
  4523.     TAG_ENTRY *tags = tag_table;
  4524.     char *indirect_info = (char *)NULL;
  4525.  
  4526.     /* Remember the `header' of this file.  The first tag in the file is
  4527.        the bottom of the header; the top of the file is the start. */
  4528.     the_header = (char *)xmalloc (1 + (header_size = tags->position));
  4529.     memcpy (the_header, the_file, header_size);
  4530.  
  4531.     while (tags)
  4532.       {
  4533.     int file_top, file_bot, limit;
  4534.  
  4535.     /* Have to include the Control-_. */
  4536.     file_top = file_bot = tags->position;
  4537.     limit = file_top + size;
  4538.  
  4539.     /* If the rest of this file is only one node, then
  4540.        that is the entire subfile. */
  4541.     if (!tags->next_ent)
  4542.       {
  4543.         int i = tags->position + 1;
  4544.         char last_char = the_file[i];
  4545.  
  4546.         while (i < fileinfo.st_size)
  4547.           {
  4548.         if ((the_file[i] == '\037') &&
  4549.             ((last_char == '\n') ||
  4550.              (last_char == '\014')))
  4551.           break;
  4552.         else
  4553.           last_char = the_file[i];
  4554.         i++;
  4555.           }
  4556.         file_bot = i;
  4557.         tags = tags->next_ent;
  4558.         goto write_region;
  4559.       }
  4560.  
  4561.     /* Otherwise, find the largest number of nodes that can fit in
  4562.        this subfile. */
  4563.     for (; tags; tags = tags->next_ent)
  4564.       {
  4565.         if (!tags->next_ent)
  4566.           {
  4567.         /* This entry is the last node.  Search forward for the end
  4568.                of this node, and that is the end of this file. */
  4569.         int i = tags->position + 1;
  4570.         char last_char = the_file[i];
  4571.  
  4572.         while (i < fileinfo.st_size)
  4573.           {
  4574.             if ((the_file[i] == '\037') &&
  4575.             ((last_char == '\n') ||
  4576.              (last_char == '\014')))
  4577.               break;
  4578.             else
  4579.               last_char = the_file[i];
  4580.             i++;
  4581.           }
  4582.         file_bot = i;
  4583.  
  4584.         if (file_bot < limit)
  4585.           {
  4586.             tags = tags->next_ent;
  4587.             goto write_region;
  4588.           }
  4589.         else
  4590.           {
  4591.             /* Here we want to write out everything before the last
  4592.                node, and then write the last node out in a file
  4593.                by itself. */
  4594.             file_bot = tags->position;
  4595.             goto write_region;
  4596.           }
  4597.           }
  4598.  
  4599.         if (tags->next_ent->position > limit)
  4600.           {
  4601.         if (tags->position == file_top)
  4602.           tags = tags->next_ent;
  4603.  
  4604.         file_bot = tags->position;
  4605.  
  4606.           write_region:
  4607.         {
  4608.           int fd;
  4609.           char *split_filename;
  4610.  
  4611.           split_filename = (char *) xmalloc
  4612.             (10 + strlen (root_pathname) + strlen (root_filename));
  4613.           sprintf
  4614.             (split_filename,
  4615.              "%s%s-%d", root_pathname, root_filename, which_file);
  4616.  
  4617.           fd = open
  4618.             (split_filename, O_WRONLY | O_TRUNC | O_CREAT, 0666);
  4619.  
  4620.           if ((fd < 0) ||
  4621.               (write (fd, the_header, header_size) != header_size) ||
  4622.               (write (fd, the_file + file_top, file_bot - file_top)
  4623.                != (file_bot - file_top)) ||
  4624.               ((close (fd)) < 0))
  4625.             {
  4626.               perror (split_filename);
  4627.               if (fd != -1)
  4628.             close (fd);
  4629.               exit (FATAL);
  4630.             }
  4631.  
  4632.           if (!indirect_info)
  4633.             {
  4634.               indirect_info = the_file + file_top;
  4635.               sprintf (indirect_info, "\037\nIndirect:\n");
  4636.               indirect_info += strlen (indirect_info);
  4637.             }
  4638.  
  4639.           sprintf (indirect_info, "%s-%d: %d\n",
  4640.                root_filename, which_file, file_top);
  4641.  
  4642.           free (split_filename);
  4643.           indirect_info += strlen (indirect_info);
  4644.           which_file++;
  4645.           break;
  4646.         }
  4647.           }
  4648.       }
  4649.       }
  4650.  
  4651.     /* We have sucessfully created the subfiles.  Now write out the
  4652.        original again.  We must use `output_stream', or
  4653.        write_tag_table_indirect () won't know where to place the output. */
  4654.     output_stream = fopen (filename, "w");
  4655.     if (!output_stream)
  4656.       {
  4657.     perror (filename);
  4658.     exit (FATAL);
  4659.       }
  4660.  
  4661.     {
  4662.       int distance = indirect_info - the_file;
  4663.       fwrite (the_file, 1, distance, output_stream);
  4664.  
  4665.       /* Inhibit newlines. */
  4666.       paragraph_is_open = 0;
  4667.  
  4668.       write_tag_table_indirect ();
  4669.       fclose (output_stream);
  4670.       free (the_header);
  4671.       free (the_file);
  4672.       return;
  4673.     }
  4674.   }
  4675. }
  4676.  
  4677. /* Some menu hacking.  This is used to remember menu references while
  4678.    reading the input file.  After the output file has been written, if
  4679.    validation is on, then we use the contents of NODE_REFERENCES as a
  4680.    list of nodes to validate. */
  4681. char *
  4682. reftype_type_string (type)
  4683.      enum reftype type;
  4684. {
  4685.   switch (type)
  4686.     {
  4687.     case menu_reference:
  4688.       return ("Menu");
  4689.     case followed_reference:
  4690.       return ("Followed-Reference");
  4691.     default:
  4692.       return ("Internal-bad-reference-type");
  4693.     }
  4694. }
  4695.  
  4696. /* Remember this node name for later validation use. */
  4697. remember_node_reference (node, line, type)
  4698.      char *node;
  4699.      int line;
  4700.      enum reftype type;
  4701. {
  4702.   NODE_REF *temp = (NODE_REF *) xmalloc (sizeof (NODE_REF));
  4703.  
  4704.   temp->next = node_references;
  4705.   temp->node = savestring (node);
  4706.   temp->line_no = line;
  4707.   temp->section = current_section;
  4708.   temp->type = type;
  4709.   temp->containing_node = savestring (current_node);
  4710.   temp->filename = node_filename;
  4711.  
  4712.   node_references = temp;
  4713. }
  4714.  
  4715. validate_other_references (ref_list)
  4716.      register NODE_REF *ref_list;
  4717. {
  4718.   char *old_input_filename = input_filename;
  4719.  
  4720.   while (ref_list != (NODE_REF *) NULL)
  4721.     {
  4722.       input_filename = ref_list->filename;
  4723.       validate (ref_list->node, ref_list->line_no,
  4724.         reftype_type_string (ref_list->type));
  4725.       ref_list = ref_list->next;
  4726.     }
  4727.   input_filename = old_input_filename;
  4728. }
  4729.  
  4730. /* Find NODE in REF_LIST. */
  4731. NODE_REF *
  4732. find_node_reference (node, ref_list)
  4733.      char *node;
  4734.      register NODE_REF *ref_list;
  4735. {
  4736.   while (ref_list)
  4737.     {
  4738.       if (strcmp (node, ref_list->node) == 0)
  4739.     break;
  4740.       ref_list = ref_list->next;
  4741.     }
  4742.   return (ref_list);
  4743. }
  4744.  
  4745. free_node_references ()
  4746. {
  4747.   register NODE_REF *list, *temp;
  4748.  
  4749.   list = node_references;
  4750.  
  4751.   while (list)
  4752.     {
  4753.       temp = list;
  4754.       free (list->node);
  4755.       free (list->containing_node);
  4756.       list = list->next;
  4757.       free (temp);
  4758.     }
  4759.   node_references = (NODE_REF *) NULL;
  4760. }
  4761.  
  4762.   /* This function gets called at the start of every line while inside of
  4763.      a menu.  It checks to see if the line starts with "* ", and if so,
  4764.      remembers the node reference that this menu refers to.
  4765.      input_text_offset is at the \n just before the line start. */
  4766. #define menu_starter "* "
  4767. char *
  4768. glean_node_from_menu (remember_reference)
  4769.      int remember_reference;
  4770. {
  4771.   int i, orig_offset = input_text_offset;
  4772.   char *nodename;
  4773.  
  4774.   if (strncmp (&input_text[input_text_offset + 1],
  4775.            menu_starter,
  4776.            strlen (menu_starter)) != 0)
  4777.     return ((char *)NULL);
  4778.   else
  4779.     input_text_offset += strlen (menu_starter) + 1;
  4780.  
  4781.   get_until_in_line (":", &nodename);
  4782.   if (curchar () == ':')
  4783.     input_text_offset++;
  4784.   canon_white (nodename);
  4785.  
  4786.   if (curchar () == ':')
  4787.     goto save_node;
  4788.  
  4789.   free (nodename);
  4790.   get_rest_of_line (&nodename);
  4791.  
  4792.   /* Special hack: If the nodename follows the menu item name,
  4793.      then we have to read the rest of the line in order to find
  4794.      out what the nodename is.  But we still have to read the
  4795.      line later, in order to process any formatting commands that
  4796.      might be present.  So un-count the carriage return that has just
  4797.      been counted. */
  4798.   line_number--;
  4799.  
  4800.   isolate_nodename (nodename);
  4801.  
  4802. save_node:
  4803.   input_text_offset = orig_offset;
  4804.   normalize_node_name (nodename);
  4805.   i = strlen (nodename);
  4806.   if (i && nodename[i - 1] == ':')
  4807.     nodename[i - 1] = '\0';
  4808.  
  4809.   if (remember_reference)
  4810.     {
  4811.       remember_node_reference (nodename, line_number, menu_reference);
  4812.       free (nodename);
  4813.       return ((char *)NULL);
  4814.     }
  4815.   else
  4816.     return (nodename);
  4817. }
  4818.  
  4819. static void
  4820. isolate_nodename (nodename)
  4821.      char *nodename;
  4822. {
  4823.   register int i, c;
  4824.   int paren_seen, paren;
  4825.  
  4826.   if (!nodename)
  4827.     return;
  4828.  
  4829.   canon_white (nodename);
  4830.   paren_seen = paren = i = 0;
  4831.  
  4832.   if (*nodename == '.' || !*nodename)
  4833.     {
  4834.       *nodename = '\0';
  4835.       return;
  4836.     }
  4837.  
  4838.   if (*nodename == '(')
  4839.     {
  4840.       paren++;
  4841.       paren_seen++;
  4842.       i++;
  4843.     }
  4844.  
  4845.   for (; c = nodename[i]; i++)
  4846.     {
  4847.       if (paren)
  4848.     {
  4849.       if (c == '(')
  4850.         paren++;
  4851.       else if (c == ')')
  4852.         paren--;
  4853.  
  4854.       continue;
  4855.     }
  4856.  
  4857.       /* If the character following the close paren is a space, then this
  4858.      node has no more characters associated with it. */
  4859.       if (c == '\t' ||
  4860.       c == '\n' ||
  4861.       c == ','  ||
  4862.       ((paren_seen && nodename[i - 1] == ')') &&
  4863.        (c == ' ' || c == '.')) ||
  4864.       (c == '.' &&
  4865.        ((!nodename[i + 1] ||
  4866.          (cr_or_whitespace (nodename[i + 1])) ||
  4867.          (nodename[i + 1] == ')')))))
  4868.     break;
  4869.     }
  4870.   nodename[i] = '\0';
  4871. }
  4872.  
  4873. cm_menu ()
  4874. {
  4875.   begin_insertion (menu);
  4876. }
  4877.  
  4878.  
  4879. /* **************************************************************** */
  4880. /*                                    */
  4881. /*            Cross Reference Hacking                */
  4882. /*                                    */
  4883. /* **************************************************************** */
  4884.  
  4885. char *
  4886. get_xref_token ()
  4887. {
  4888.   char *string;
  4889.  
  4890.   get_until_in_braces (",", &string);
  4891.   if (curchar () == ',')
  4892.     input_text_offset++;
  4893.   fix_whitespace (string);
  4894.   return (string);
  4895. }
  4896.  
  4897. int px_ref_flag = 0;        /* Controls initial output string. */
  4898.  
  4899. /* Make a cross reference. */
  4900. cm_xref (arg)
  4901. {
  4902.   if (arg == START)
  4903.     {
  4904.       char *arg1, *arg2, *arg3, *arg4, *arg5;
  4905.  
  4906.       arg1 = get_xref_token ();
  4907.       arg2 = get_xref_token ();
  4908.       arg3 = get_xref_token ();
  4909.       arg4 = get_xref_token ();
  4910.       arg5 = get_xref_token ();
  4911.  
  4912.       add_word_args ("%s", px_ref_flag ? "*note " : "*Note ");
  4913.  
  4914.       if (*arg5 || *arg4)
  4915.     {
  4916.       char *node_name;
  4917.  
  4918.       if (!*arg2)
  4919.         {
  4920.           if (*arg3)
  4921.         node_name = arg3;
  4922.           else
  4923.         node_name = arg1;
  4924.         }
  4925.       else
  4926.         node_name = arg2;
  4927.  
  4928.       execute_string ("%s: (%s)%s", node_name, arg4, arg1);
  4929.       return;
  4930.     }
  4931.       else
  4932.     remember_node_reference (arg1, line_number, followed_reference);
  4933.  
  4934.       if (*arg3)
  4935.     {
  4936.       if (!*arg2)
  4937.         execute_string ("%s: %s", arg3, arg1);
  4938.       else
  4939.         execute_string ("%s: %s", arg2, arg1);
  4940.       return;
  4941.     }
  4942.  
  4943.       if (*arg2)
  4944.     execute_string ("%s: %s", arg2, arg1);
  4945.       else
  4946.     execute_string ("%s::", arg1);
  4947.     }
  4948.   else
  4949.     {
  4950.  
  4951.       /* Check to make sure that the next non-whitespace character is either
  4952.          a period or a comma. input_text_offset is pointing at the "}" which
  4953.          ended the xref or pxref command. */
  4954.       int temp = input_text_offset + 1;
  4955.  
  4956.       if (output_paragraph[output_paragraph_offset - 2] == ':' &&
  4957.       output_paragraph[output_paragraph_offset - 1] == ':')
  4958.     return;
  4959.       while (temp < size_of_input_text)
  4960.     {
  4961.       if (cr_or_whitespace (input_text[temp]))
  4962.         temp++;
  4963.       else
  4964.         {
  4965.           if (input_text[temp] == '.' ||
  4966.           input_text[temp] == ',' ||
  4967.           input_text[temp] == '\t')
  4968.         return;
  4969.           else
  4970.         {
  4971.           line_error (
  4972.         "Cross-reference must be terminated with a period or a comma");
  4973.           return;
  4974.         }
  4975.         }
  4976.     }
  4977.     }
  4978. }
  4979.  
  4980. cm_pxref (arg)
  4981.      int arg;
  4982. {
  4983.   if (arg == START)
  4984.     {
  4985.       px_ref_flag++;
  4986.       cm_xref (arg);
  4987.       px_ref_flag--;
  4988.     }
  4989.   else
  4990.     add_char ('.');
  4991. }
  4992.  
  4993. cm_inforef (arg)
  4994.      int arg;
  4995. {
  4996.   if (arg == START)
  4997.     {
  4998.       char *node, *pname, *file;
  4999.  
  5000.       node = get_xref_token ();
  5001.       pname = get_xref_token ();
  5002.       file = get_xref_token ();
  5003.  
  5004.       execute_string ("*note %s: (%s)%s", pname, file, node);
  5005.     }
  5006. }
  5007.  
  5008. /* **************************************************************** */
  5009. /*                                    */
  5010. /*            Insertion Command Stubs                */
  5011. /*                                    */
  5012. /* **************************************************************** */
  5013.  
  5014. cm_quotation ()
  5015. {
  5016.   begin_insertion (quotation);
  5017. }
  5018.  
  5019. cm_example ()
  5020. {
  5021.   begin_insertion (example);
  5022. }
  5023.  
  5024. cm_smallexample ()
  5025. {
  5026.   begin_insertion (smallexample);
  5027. }
  5028.  
  5029. cm_lisp ()
  5030. {
  5031.   begin_insertion (lisp);
  5032. }
  5033.  
  5034. cm_smalllisp ()
  5035. {
  5036.   begin_insertion (smalllisp);
  5037. }
  5038.  
  5039. /* @cartouche/@end cartouche draws box with rounded corners in
  5040.    TeX output.  Right now, just a NOP insertion. */
  5041. cm_cartouche ()
  5042. {
  5043.   begin_insertion (cartouche);
  5044. }
  5045.  
  5046. cm_format ()
  5047. {
  5048.   begin_insertion (format);
  5049. }
  5050.  
  5051. cm_display ()
  5052. {
  5053.   begin_insertion (display);
  5054. }
  5055.  
  5056. cm_itemize ()
  5057. {
  5058.   begin_insertion (itemize);
  5059. }
  5060.  
  5061. cm_enumerate ()
  5062. {
  5063.   do_enumeration (enumerate, "1");
  5064. }
  5065.  
  5066. /* Start an enumeration insertion of type TYPE.  If the user supplied
  5067.    no argument on the line, then use DEFAULT_STRING as the initial string. */
  5068. do_enumeration (type, default_string)
  5069.      int type;
  5070.      char *default_string;
  5071. {
  5072.   get_until_in_line (".", &enumeration_arg);
  5073.   canon_white (enumeration_arg);
  5074.  
  5075.   if (!*enumeration_arg)
  5076.     {
  5077.       free (enumeration_arg);
  5078.       enumeration_arg = savestring (default_string);
  5079.     }
  5080.  
  5081.   if (!isdigit (*enumeration_arg) && !isletter (*enumeration_arg))
  5082.     {
  5083.       warning ("%s requires a letter or a digit", insertion_type_pname (type));
  5084.  
  5085.       switch (type)
  5086.     {
  5087.     case enumerate:
  5088.       default_string = "1";
  5089.       break;
  5090.     }
  5091.       enumeration_arg = savestring (default_string);
  5092.     }
  5093.   begin_insertion (type);
  5094. }
  5095.  
  5096. cm_table ()
  5097. {
  5098.   begin_insertion (table);
  5099. }
  5100.  
  5101. cm_ftable ()
  5102. {
  5103.   begin_insertion (ftable);
  5104. }
  5105.  
  5106. cm_vtable ()
  5107. {
  5108.   begin_insertion (vtable);
  5109. }
  5110.  
  5111. cm_group ()
  5112. {
  5113.   begin_insertion (group);
  5114. }
  5115.  
  5116. cm_ifinfo ()
  5117. {
  5118.   begin_insertion (ifinfo);
  5119. }
  5120.  
  5121. /* Begin an insertion where the lines are not filled or indented. */
  5122. cm_flushleft ()
  5123. {
  5124.   begin_insertion (flushleft);
  5125. }
  5126.  
  5127. /* Begin an insertion where the lines are not filled, and each line is
  5128.    forced to the right-hand side of the page. */
  5129. cm_flushright ()
  5130. {
  5131.   begin_insertion (flushright);
  5132. }
  5133.  
  5134.  
  5135. /* **************************************************************** */
  5136. /*                                    */
  5137. /*              Conditional Handling                */
  5138. /*                                    */
  5139. /* **************************************************************** */
  5140.  
  5141. /* A structure which contains `defined' variables. */
  5142. typedef struct _defines {
  5143.   struct _defines *next;
  5144.   char *name;
  5145.   char *value;
  5146. } DEFINE;
  5147.  
  5148. /* The linked list of `set' defines. */
  5149. DEFINE *defines = (DEFINE *)NULL;
  5150.  
  5151. /* Add NAME to the list of `set' defines. */
  5152. set (name, value)
  5153.      char *name;
  5154.      char *value;
  5155. {
  5156.   DEFINE *temp;
  5157.  
  5158.   for (temp = defines; temp; temp = temp->next)
  5159.     if (strcmp (name, temp->name) == 0)
  5160.       {
  5161.     free (temp->value);
  5162.     temp->value = savestring (value);
  5163.     return;
  5164.       }
  5165.  
  5166.   temp = (DEFINE *)xmalloc (sizeof (DEFINE));
  5167.   temp->next = defines;
  5168.   temp->name = savestring (name);
  5169.   temp->value = savestring (value);
  5170.   defines = temp;
  5171. }
  5172.  
  5173. /* Remove NAME from the list of `set' defines. */
  5174. clear (name)
  5175.      char *name;
  5176. {
  5177.   register DEFINE *temp, *last;
  5178.  
  5179.   last = (DEFINE *)NULL;
  5180.   temp = defines;
  5181.  
  5182.   while (temp)
  5183.     {
  5184.       if (strcmp (temp->name, name) == 0)
  5185.     {
  5186.       if (last)
  5187.         last->next = temp->next;
  5188.       else
  5189.         defines = temp->next;
  5190.  
  5191.       free (temp->name);
  5192.       free (temp->value);
  5193.       free (temp);
  5194.       break;
  5195.     }
  5196.       last = temp;
  5197.       temp = temp->next;
  5198.     }
  5199. }
  5200.  
  5201. /* Return the value of NAME.  The return value is NULL if NAME is unset. */
  5202. char *
  5203. set_p (name)
  5204.      char *name;
  5205. {
  5206.   register DEFINE *temp;
  5207.  
  5208.   for (temp = defines; temp; temp = temp->next)
  5209.     if (strcmp (temp->name, name) == 0)
  5210.       return (temp->value);
  5211.  
  5212.   return ((char *)NULL);
  5213. }
  5214.  
  5215. /* Conditionally parse based on the current command name. */
  5216. command_name_condition ()
  5217. {
  5218.   char *discarder;
  5219.  
  5220.   discarder = (char *)xmalloc (8 + strlen (command));
  5221.  
  5222.   sprintf (discarder, "\n@end %s", command);
  5223.   discard_until (discarder);
  5224.   discard_until ("\n");
  5225.  
  5226.   free (discarder);
  5227. }
  5228.  
  5229. /* Create a variable whose name appears as the first word on this line. */
  5230. cm_set ()
  5231. {
  5232.   handle_variable (SET);
  5233. }
  5234.  
  5235. /* Remove a variable whose name appears as the first word on this line. */
  5236. cm_clear ()
  5237. {
  5238.   handle_variable (CLEAR);
  5239. }
  5240.  
  5241. cm_ifset ()
  5242. {
  5243.   handle_variable (IFSET);
  5244. }
  5245.  
  5246. cm_ifclear ()
  5247. {
  5248.   handle_variable (IFCLEAR);
  5249. }
  5250.  
  5251. cm_value (arg, start_pos, end_pos)
  5252.      int arg, start_pos, end_pos;
  5253. {
  5254.   if (arg == END)
  5255.     {
  5256.       char *name, *value;
  5257.       name = (char *)&output_paragraph[start_pos];
  5258.       output_paragraph[end_pos] = '\0';
  5259.       name = savestring (name);
  5260.       value = set_p (name);
  5261.       output_column -= end_pos - start_pos;
  5262.       output_paragraph_offset = start_pos;
  5263.  
  5264.       if (value)
  5265.         execute_string ("%s", value);
  5266.       else
  5267.     add_word_args ("{No Value For \"%s\"}", name);
  5268.  
  5269.       free (name);
  5270.     }
  5271. }
  5272.  
  5273. /* Set, clear, or conditionalize based on ACTION. */
  5274. handle_variable (action)
  5275.      int action;
  5276. {
  5277.   char *name;
  5278.  
  5279.   get_rest_of_line (&name);
  5280.   backup_input_pointer ();
  5281.   canon_white (name);
  5282.   handle_variable_internal (action, name);
  5283.   free (name);
  5284. }
  5285.  
  5286. handle_variable_internal (action, name)
  5287.      int action;
  5288.      char *name;
  5289. {
  5290.   char *temp;
  5291.   int delimiter, additional_text_present = 0;
  5292.  
  5293.   /* Only the first word of NAME is a valid tag. */
  5294.   temp = name;
  5295.   delimiter = 0;
  5296.   while (*temp && (delimiter || !whitespace (*temp)))
  5297.     {
  5298. #if defined (SET_WITH_EQUAL)
  5299.       if (*temp == '"' || *temp == '\'')
  5300.     {
  5301.       if (*temp == delimiter)
  5302.         delimiter = 0;
  5303.       else
  5304.         delimiter = *temp;
  5305.     }
  5306. #endif /* SET_WITH_EQUAL */
  5307.       temp++;
  5308.     }
  5309.  
  5310.   if (*temp)
  5311.     additional_text_present++;
  5312.  
  5313.   *temp = '\0';
  5314.  
  5315.   if (!*name)
  5316.     line_error ("@%s requires a name", command);
  5317.   else
  5318.     {
  5319.       switch (action)
  5320.     {
  5321.     case SET:
  5322.       {
  5323.         char *value;
  5324.  
  5325. #if defined (SET_WITH_EQUAL)
  5326.         /* Allow a value to be saved along with a variable.  The value is
  5327.            the text following an `=' sign in NAME, if any is present. */
  5328.  
  5329.         for (value = name; *value && *value != '='; value++);
  5330.  
  5331.         if (*value)
  5332.           *value++ = '\0';
  5333.  
  5334.         if (*value == '"' || *value == '\'')
  5335.           {
  5336.         value++;
  5337.         value[strlen (value) - 1] = '\0';
  5338.           }
  5339.  
  5340. #else /* !SET_WITH_EQUAL */
  5341.         /* The VALUE of NAME is the remainder of the line sans
  5342.            whitespace. */
  5343.         if (additional_text_present)
  5344.           {
  5345.         value = temp + 1;
  5346.         canon_white (value);
  5347.           }
  5348.         else
  5349.           value = "";
  5350. #endif /* !SET_WITH_VALUE */
  5351.  
  5352.         set (name, value);
  5353.       }
  5354.       break;
  5355.  
  5356.     case CLEAR:
  5357.       clear (name);
  5358.       break;
  5359.  
  5360.     case IFSET:
  5361.     case IFCLEAR:
  5362.       /* If IFSET and NAME is not set, or if IFCLEAR and NAME is set,
  5363.          read lines from the the file until we reach a matching
  5364.          "@end CONDITION".  This means that we only take note of
  5365.          "@ifset/clear" and "@end" commands. */
  5366.       {
  5367.         char condition[8];
  5368.         int condition_len;
  5369.  
  5370.         if (action == IFSET)
  5371.           strcpy (condition, "ifset");
  5372.         else
  5373.           strcpy (condition, "ifclear");
  5374.  
  5375.         condition_len = strlen (condition);
  5376.  
  5377.       if ((action == IFSET && !set_p (name)) ||
  5378.           (action == IFCLEAR && set_p (name)))
  5379.         {
  5380.           int level = 0, done = 0;
  5381.  
  5382.           while (!done)
  5383.         {
  5384.           char *freeable_line, *line;
  5385.  
  5386.           get_rest_of_line (&freeable_line);
  5387.  
  5388.           for (line = freeable_line; whitespace (*line); line++);
  5389.  
  5390.           if (*line == COMMAND_PREFIX &&
  5391.               (strncmp (line + 1, condition, condition_len) == 0))
  5392.             level++;
  5393.           else if (strncmp (line, "@end", 4) == 0)
  5394.             {
  5395.               char *cname = line + 4;
  5396.               char *temp;
  5397.  
  5398.               while (*cname && whitespace (*cname))
  5399.             cname++;
  5400.               temp = cname;
  5401.  
  5402.               while (*temp && !whitespace (*temp))
  5403.             temp++;
  5404.               *temp = '\0';
  5405.  
  5406.               if (strcmp (cname, condition) == 0)
  5407.             {
  5408.               if (!level)
  5409.                 {
  5410.                   done = 1;
  5411.                 }
  5412.               else
  5413.                 level--;
  5414.             }
  5415.             }
  5416.           free (freeable_line);
  5417.         }
  5418.           /* We found the end of a false @ifset/ifclear.  If we are
  5419.          in a menu, back up over the newline that ends the ifset,
  5420.          since that newline may also begin the next menu entry. */
  5421.           break;
  5422.         }
  5423.       else
  5424.         {
  5425.           if (action == IFSET)
  5426.         begin_insertion (ifset);
  5427.           else
  5428.         begin_insertion (ifclear);
  5429.         }
  5430.       }
  5431.       break;
  5432.     }
  5433.     }
  5434. }
  5435.  
  5436. /* **************************************************************** */
  5437. /*                                    */
  5438. /*            @itemx, @item                    */
  5439. /*                                    */
  5440. /* **************************************************************** */
  5441.  
  5442. /* Non-zero means a string is in execution, as opposed to a file. */
  5443. int executing_string = 0;
  5444.  
  5445. /* Execute the string produced by formatting the ARGs with FORMAT.  This
  5446.    is like submitting a new file with @include. */
  5447. #if defined (HAVE_VARARGS_H) && defined(HAVE_VSPRINTF)
  5448.  
  5449. execute_string (va_alist)
  5450.      va_dcl
  5451. {
  5452.   static char temp_string[4000];
  5453.   char *format;
  5454.   va_list args;
  5455.  
  5456.   va_start (args);
  5457.   format = va_arg (args, char *);
  5458.   vsprintf (temp_string, format, args);
  5459.   va_end (args);
  5460.  
  5461. #else /* !(HAVE_VARARGS_H && HAVE_VSPRINTF) */
  5462.  
  5463. execute_string (format, arg1, arg2, arg3, arg4, arg5)
  5464.      char *format;
  5465. {
  5466.   static char temp_string[4000];
  5467.   sprintf (temp_string, format, arg1, arg2, arg3, arg4, arg5);
  5468.  
  5469. #endif /* !(HAVE_VARARGS_H && HAVE_VSPRINTF) */
  5470.  
  5471.   strcat (temp_string, "@bye\n");
  5472.   pushfile ();
  5473.   input_text_offset = 0;
  5474.   input_text = temp_string;
  5475.   input_filename = savestring (input_filename);
  5476.   size_of_input_text = strlen (temp_string);
  5477.  
  5478.   executing_string++;
  5479.   reader_loop ();
  5480.  
  5481.   popfile ();
  5482.   executing_string--;
  5483.  
  5484.   free_and_clear (&command);
  5485.   command = savestring ("not bye");
  5486. }
  5487.  
  5488. int itemx_flag = 0;
  5489.  
  5490. cm_itemx ()
  5491. {
  5492.   itemx_flag++;
  5493.   cm_item ();
  5494.   itemx_flag--;
  5495. }
  5496.  
  5497. cm_item ()
  5498. {
  5499.   char *rest_of_line, *item_func;
  5500.  
  5501.   /* Can only hack "@item" while inside of an insertion. */
  5502.   if (insertion_level)
  5503.     {
  5504.       INSERTION_ELT *stack = insertion_stack;
  5505.       int original_input_text_offset;
  5506.  
  5507.       skip_whitespace ();
  5508.       original_input_text_offset = input_text_offset;
  5509.  
  5510.       get_rest_of_line (&rest_of_line);
  5511.       canon_white (rest_of_line);
  5512.       item_func = current_item_function ();
  5513.  
  5514.       /* Okay, do the right thing depending on which insertion function
  5515.      is active. */
  5516.  
  5517.     switch_top:
  5518.       switch (stack->insertion)
  5519.     {
  5520.     case ifinfo:
  5521.     case ifset:
  5522.     case ifclear:
  5523.     case cartouche:
  5524.       stack = stack->next;
  5525.       if (!stack)
  5526.         goto no_insertion;
  5527.       else
  5528.         goto switch_top;
  5529.       break;
  5530.  
  5531.     case menu:
  5532.     case quotation:
  5533.     case example:
  5534.     case smallexample:
  5535.     case lisp:
  5536.     case format:
  5537.     case display:
  5538.     case group:
  5539.       line_error ("The `@%s' command is meaningless within a `@%s' block",
  5540.               command,
  5541.               insertion_type_pname (current_insertion_type ()));
  5542.       break;
  5543.  
  5544.     case itemize:
  5545.     case enumerate:
  5546.       if (itemx_flag)
  5547.         {
  5548.           line_error ("@itemx is not meaningful inside of a `%s' block",
  5549.               insertion_type_pname (current_insertion_type ()));
  5550.         }
  5551.       else
  5552.         {
  5553.           start_paragraph ();
  5554.           kill_self_indent (-1);
  5555.           filling_enabled = indented_fill = 1;
  5556.  
  5557.           if (current_insertion_type () == itemize)
  5558.         {
  5559.           indent (output_column = current_indent - 2);
  5560.  
  5561.           /* I need some way to determine whether this command
  5562.              takes braces or not.  I believe the user can type
  5563.              either "@bullet" or "@bullet{}".  Of course, they
  5564.              can also type "o" or "#" or whatever else they want. */
  5565.           if (item_func && *item_func)
  5566.             {
  5567.               if (*item_func == '@')
  5568.             if (item_func[strlen (item_func) - 1] != '}')
  5569.               execute_string ("%s{}", item_func);
  5570.             else
  5571.               execute_string ("%s", item_func);
  5572.               else
  5573.             execute_string ("%s", item_func);
  5574.             }
  5575.           insert (' ');
  5576.           output_column++;
  5577.         }
  5578.           else
  5579.         enumerate_item ();
  5580.  
  5581.           /* Special hack.  This makes close paragraph ignore you until
  5582.          the start_paragraph () function has been called. */
  5583.           must_start_paragraph = 1;
  5584.  
  5585.           /* Ultra special hack.  It appears that some people incorrectly
  5586.          place text directly after the @item, instead of on a new line
  5587.          by itself.  This happens to work in TeX, so I make it work
  5588.          here. */
  5589.           if (*rest_of_line)
  5590.         {
  5591.           line_number--;
  5592.           input_text_offset = original_input_text_offset;
  5593.         }
  5594.         }
  5595.       break;
  5596.  
  5597.     case table:
  5598.     case ftable:
  5599.     case vtable:
  5600.       {
  5601.         /* Get rid of extra characters. */
  5602.         kill_self_indent (-1);
  5603.  
  5604.         /* close_paragraph () almost does what we want.  The problem
  5605.            is when paragraph_is_open, and last_char_was_newline, and
  5606.            the last newline has been turned into a space, because
  5607.            filling_enabled. I handle it here. */
  5608.         if (last_char_was_newline && filling_enabled && paragraph_is_open)
  5609.           insert ('\n');
  5610.         close_paragraph ();
  5611.  
  5612. #if defined (INDENT_PARAGRAPHS_IN_TABLE)
  5613.         /* Indent on a new line, but back up one indentation level. */
  5614.         {
  5615.           int t;
  5616.  
  5617.           t = inhibit_paragraph_indentation;
  5618.           inhibit_paragraph_indentation = 1;
  5619.           /* At this point, inserting any non-whitespace character will
  5620.          force the existing indentation to be output. */
  5621.           add_char ('i');
  5622.           inhibit_paragraph_indentation = t;
  5623.         }
  5624. #else /* !INDENT_PARAGRAPHS_IN_TABLE */
  5625.         add_char ('i');
  5626. #endif /* !INDENT_PARAGRAPHS_IN_TABLE */
  5627.  
  5628.         output_paragraph_offset--;
  5629.         kill_self_indent (default_indentation_increment + 1);
  5630.  
  5631.         /* Add item's argument to the line. */
  5632.         filling_enabled = 0;
  5633.         if (item_func && *item_func)
  5634.            execute_string ("%s{%s}", item_func, rest_of_line);
  5635.          else
  5636.            execute_string ("%s", rest_of_line);
  5637.  
  5638.         if (current_insertion_type () == ftable)
  5639.           execute_string ("@findex %s\n", rest_of_line);
  5640.  
  5641.         if (current_insertion_type () == vtable)
  5642.           execute_string ("@vindex %s\n", rest_of_line);
  5643.  
  5644.         /* Start a new line, and let start_paragraph ()
  5645.            do the indenting of it for you. */
  5646.         close_single_paragraph ();
  5647.         indented_fill = filling_enabled = 1;
  5648.       }
  5649.     }
  5650.       free (rest_of_line);
  5651.     }
  5652.   else
  5653.     {
  5654.     no_insertion:
  5655.       line_error ("@%s found outside of an insertion block", command);
  5656.     }
  5657. }
  5658.  
  5659.  
  5660. /* **************************************************************** */
  5661. /*                                    */
  5662. /*            Defun and Friends                   */
  5663. /*                                    */
  5664. /* **************************************************************** */
  5665.  
  5666. #define DEFUN_SELF_DELIMITING(c)                    \
  5667.   (((c) == '(')                                \
  5668.    || ((c) == ')')                            \
  5669.    || ((c) == '[')                            \
  5670.    || ((c) == ']'))
  5671.  
  5672. struct token_accumulator
  5673. {
  5674.   unsigned int length;
  5675.   unsigned int index;
  5676.   char **tokens;
  5677. };
  5678.  
  5679. void
  5680. initialize_token_accumulator (accumulator)
  5681.      struct token_accumulator *accumulator;
  5682. {
  5683.   (accumulator->length) = 0;
  5684.   (accumulator->index) = 0;
  5685.   (accumulator->tokens) = NULL;
  5686. }
  5687.  
  5688. void
  5689. accumulate_token (accumulator, token)
  5690.      struct token_accumulator *accumulator;
  5691.      char *token;
  5692. {
  5693.   if ((accumulator->index) >= (accumulator->length))
  5694.     {
  5695.       (accumulator->length) += 10;
  5696.       (accumulator->tokens) = (char **) xrealloc
  5697.     (accumulator->tokens, (accumulator->length * sizeof (char *)));
  5698.     }
  5699.   accumulator->tokens[accumulator->index] = token;
  5700.   accumulator->index += 1;
  5701. }
  5702.  
  5703. char *
  5704. copy_substring (start, end)
  5705.      char *start;
  5706.      char *end;
  5707. {
  5708.   char *result, *scan, *scan_result;
  5709.  
  5710.   result = (char *) xmalloc ((end - start) + 1);
  5711.   scan_result = result;
  5712.   scan = start;
  5713.  
  5714.   while (scan < end)
  5715.     *scan_result++ = *scan++;
  5716.  
  5717.   *scan_result = '\0';
  5718.   return (result);
  5719. }
  5720.  
  5721. /* Given `string' pointing at an open brace, skip forward and return a
  5722.    pointer to just past the matching close brace. */
  5723. int
  5724. scan_group_in_string (string_pointer)
  5725.      char **string_pointer;
  5726. {
  5727.   register int c;
  5728.   register char *scan_string;
  5729.   register unsigned int level = 1;
  5730.  
  5731.   scan_string = (*string_pointer) + 1;
  5732.  
  5733.   while (1)
  5734.     {
  5735.       if (level == 0)
  5736.     {
  5737.       (*string_pointer) = scan_string;
  5738.       return (1);
  5739.     }
  5740.       c = (*scan_string++);
  5741.       if (c == '\0')
  5742.     {
  5743.       /* Tweak line_number to compensate for fact that
  5744.          we gobbled the whole line before coming here. */
  5745.       line_number -= 1;
  5746.       line_error ("Missing `}' in @def arg");
  5747.       line_number += 1;
  5748.       (*string_pointer) = (scan_string - 1);
  5749.       return (0);
  5750.     }
  5751.       if (c == '{')
  5752.     level += 1;
  5753.       if (c == '}')
  5754.     level -= 1;
  5755.     }
  5756. }
  5757.  
  5758. /* Return a list of tokens from the contents of `string'.
  5759.    Commands and brace-delimited groups count as single tokens.
  5760.    Contiguous whitespace characters are converted to a token
  5761.    consisting of a single space. */
  5762. char **
  5763. args_from_string (string)
  5764.      char *string;
  5765. {
  5766.   struct token_accumulator accumulator;
  5767.   register char *scan_string = string;
  5768.   char *token_start, *token_end;
  5769.  
  5770.   initialize_token_accumulator (&accumulator);
  5771.  
  5772.   while ((*scan_string) != '\0')
  5773.     {
  5774.       /* Replace arbitrary whitespace by a single space. */
  5775.       if (whitespace (*scan_string))
  5776.     {
  5777.       scan_string += 1;
  5778.       while (whitespace (*scan_string))
  5779.         scan_string += 1;
  5780.       accumulate_token ((&accumulator), (savestring (" ")));
  5781.       continue;
  5782.     }
  5783.  
  5784.       /* Commands count as single tokens. */
  5785.       if ((*scan_string) == COMMAND_PREFIX)
  5786.     {
  5787.       token_start = scan_string;
  5788.       scan_string += 1;
  5789.       if (self_delimiting (*scan_string))
  5790.         scan_string += 1;
  5791.       else
  5792.         {
  5793.           register int c;
  5794.           while (1)
  5795.         {
  5796.           c = *scan_string++;
  5797.  
  5798.            if ((c == '\0') || (c == '{') || (whitespace (c)))
  5799.             {
  5800.               scan_string -= 1;
  5801.               break;
  5802.             }
  5803.         }
  5804.  
  5805.           if (*scan_string == '{')
  5806.         {
  5807.           char *s = scan_string;
  5808.           (void) scan_group_in_string (&s);
  5809.           scan_string = s;
  5810.         }
  5811.         }
  5812.       token_end = scan_string;
  5813.     }
  5814.  
  5815.       /* Parentheses and brackets are self-delimiting. */
  5816.       else if (DEFUN_SELF_DELIMITING (*scan_string))
  5817.     {
  5818.       token_start = scan_string;
  5819.       scan_string += 1;
  5820.       token_end = scan_string;
  5821.     }
  5822.  
  5823.       /* Open brace introduces a group that is a single token. */
  5824.       else if (*scan_string == '{')
  5825.     {
  5826.       char *s = scan_string;
  5827.       int balanced = scan_group_in_string (&s);
  5828.  
  5829.       token_start = scan_string + 1;
  5830.       scan_string = s;
  5831.       token_end = balanced ? (scan_string - 1) : scan_string;
  5832.     }
  5833.  
  5834.       /* Otherwise a token is delimited by whitespace, parentheses,
  5835.      brackets, or braces.  A token is also ended by a command. */
  5836.       else
  5837.     {
  5838.       token_start = scan_string;
  5839.  
  5840.       while (1)
  5841.         {
  5842.           register int c;
  5843.  
  5844.           c = *scan_string++;
  5845.  
  5846.           if (!c ||
  5847.           (whitespace (c) || DEFUN_SELF_DELIMITING (c) ||
  5848.            c == '{' || c == '}'))
  5849.         {
  5850.           scan_string--;
  5851.           break;
  5852.         }
  5853.  
  5854.           /* If we encounter a command imbedded within a token,
  5855.          then end the token. */
  5856.           if (c == COMMAND_PREFIX)
  5857.         {
  5858.           scan_string--;
  5859.           break;
  5860.         }
  5861.         }
  5862.       token_end = scan_string;
  5863.     }
  5864.  
  5865.       accumulate_token
  5866.     (&accumulator, copy_substring (token_start, token_end));
  5867.     }
  5868.   accumulate_token (&accumulator, NULL);
  5869.   return (accumulator.tokens);
  5870. }
  5871.  
  5872. void
  5873. process_defun_args (defun_args, auto_var_p)
  5874.      char **defun_args;
  5875.      int auto_var_p;
  5876. {
  5877.   int pending_space = 0;
  5878.  
  5879.   while (1)
  5880.     {
  5881.       char *defun_arg = *defun_args++;
  5882.  
  5883.       if (defun_arg == NULL)
  5884.     break;
  5885.  
  5886.       if (defun_arg[0] == ' ')
  5887.     {
  5888.       pending_space = 1;
  5889.       continue;
  5890.     }
  5891.  
  5892.       if (pending_space)
  5893.     {
  5894.       add_char (' ');
  5895.       pending_space = 0;
  5896.     }
  5897.  
  5898.       if (DEFUN_SELF_DELIMITING (defun_arg[0]))
  5899.     add_char (defun_arg[0]);
  5900.       else if (defun_arg[0] == '&')
  5901.     add_word (defun_arg);
  5902.       else if (defun_arg[0] == COMMAND_PREFIX)
  5903.     execute_string ("%s", defun_arg);
  5904.       else if (auto_var_p)
  5905.     execute_string ("@var{%s}", defun_arg);
  5906.       else
  5907.     add_word (defun_arg);
  5908.     }
  5909. }
  5910.  
  5911. char *
  5912. next_nonwhite_defun_arg (arg_pointer)
  5913.      char ***arg_pointer;
  5914. {
  5915.   char **scan = (*arg_pointer);
  5916.   char *arg = (*scan++);
  5917.  
  5918.   if ((arg != 0) && (*arg == ' '))
  5919.     arg = *scan++;
  5920.  
  5921.   if (arg == 0)
  5922.     scan -= 1;
  5923.  
  5924.   *arg_pointer = scan;
  5925.  
  5926.   return ((arg == 0) ? "" : arg);
  5927. }
  5928.  
  5929. /* Make the defun type insertion.
  5930.    TYPE says which insertion this is.
  5931.    X_P says not to start a new insertion if non-zero. */
  5932. void
  5933. defun_internal (type, x_p)
  5934.      enum insertion_type type;
  5935.      int x_p;
  5936. {
  5937.   enum insertion_type base_type;
  5938.   char **defun_args, **scan_args;
  5939.   char *category, *defined_name, *type_name, *type_name2;
  5940.  
  5941.   {
  5942.     char *line;
  5943.     get_rest_of_line (&line);
  5944.     defun_args = (args_from_string (line));
  5945.     free (line);
  5946.   }
  5947.  
  5948.   scan_args = defun_args;
  5949.  
  5950.   switch (type)
  5951.     {
  5952.     case defun:
  5953.       category = "Function";
  5954.       base_type = deffn;
  5955.       break;
  5956.     case defmac:
  5957.       category = "Macro";
  5958.       base_type = deffn;
  5959.       break;
  5960.     case defspec:
  5961.       category = "Special Form";
  5962.       base_type = deffn;
  5963.       break;
  5964.     case defvar:
  5965.       category = "Variable";
  5966.       base_type = defvr;
  5967.       break;
  5968.     case defopt:
  5969.       category = "User Option";
  5970.       base_type = defvr;
  5971.       break;
  5972.     case deftypefun:
  5973.       category = "Function";
  5974.       base_type = deftypefn;
  5975.       break;
  5976.     case deftypevar:
  5977.       category = "Variable";
  5978.       base_type = deftypevr;
  5979.       break;
  5980.     case defivar:
  5981.       category = "Instance Variable";
  5982.       base_type = defcv;
  5983.       break;
  5984.     case defmethod:
  5985.       category = "Method";
  5986.       base_type = defop;
  5987.       break;
  5988.     case deftypemethod:
  5989.       category = "Method";
  5990.       base_type = deftypemethod;
  5991.       break;
  5992.     default:
  5993.       category = next_nonwhite_defun_arg (&scan_args);
  5994.       base_type = type;
  5995.       break;
  5996.     }
  5997.  
  5998.   if ((base_type == deftypefn)
  5999.       || (base_type == deftypevr)
  6000.       || (base_type == defcv)
  6001.       || (base_type == defop)
  6002.       || (base_type == deftypemethod))
  6003.     type_name = next_nonwhite_defun_arg (&scan_args);
  6004.  
  6005.   if (base_type == deftypemethod)
  6006.     type_name2 = next_nonwhite_defun_arg (&scan_args);
  6007.  
  6008.   defined_name = next_nonwhite_defun_arg (&scan_args);
  6009.  
  6010.   /* This hack exists solely for the purposes of formatting the texinfo
  6011.      manual.  I couldn't think of a better way.  The token might be
  6012.      a simple @@ followed immediately by more text.  If this is the case,
  6013.      then the next defun arg is part of this one, and we should concatenate
  6014.      them. */
  6015.   if (*scan_args && **scan_args && !whitespace (**scan_args) &&
  6016.       (strcmp (defined_name, "@@") == 0))
  6017.     {
  6018.       char *tem = (char *)xmalloc (3 + strlen (scan_args[0]));
  6019.  
  6020.       sprintf (tem, "@@%s", scan_args[0]);
  6021.  
  6022.       free (scan_args[0]);
  6023.       scan_args[0] = tem;
  6024.       scan_args++;
  6025.       defined_name = tem;
  6026.     }
  6027.  
  6028.   if (!x_p)
  6029.     begin_insertion (type);
  6030.  
  6031.   /* Write the definition header line.
  6032.      This should start at the normal indentation.  */
  6033.   current_indent -= default_indentation_increment;
  6034.   start_paragraph ();
  6035.  
  6036.   switch (base_type)
  6037.     {
  6038.     case deffn:
  6039.     case defvr:
  6040.     case deftp:
  6041.       execute_string (" -- %s: %s", category, defined_name);
  6042.       break;
  6043.     case deftypefn:
  6044.     case deftypevr:
  6045.       execute_string (" -- %s: %s %s", category, type_name, defined_name);
  6046.       break;
  6047.     case defcv:
  6048.       execute_string (" -- %s of %s: %s", category, type_name, defined_name);
  6049.       break;
  6050.     case defop:
  6051.       execute_string (" -- %s on %s: %s", category, type_name, defined_name);
  6052.       break;
  6053.     case deftypemethod:
  6054.       execute_string (" -- %s on %s: %s %s", category, type_name, type_name2,
  6055.               defined_name);
  6056.       break;
  6057.     }
  6058.   current_indent += default_indentation_increment;
  6059.  
  6060.   /* Now process the function arguments, if any.
  6061.      If these carry onto the next line, they should be indented by two
  6062.      increments to distinguish them from the body of the definition,
  6063.      which is indented by one increment.  */
  6064.   current_indent += default_indentation_increment;
  6065.  
  6066.   switch (base_type)
  6067.     {
  6068.     case deffn:
  6069.     case defop:
  6070.       process_defun_args (scan_args, 1);
  6071.       break;
  6072.     case deftp:
  6073.     case deftypefn:
  6074.     case deftypemethod:
  6075.       process_defun_args (scan_args, 0);
  6076.       break;
  6077.     }
  6078.   current_indent -= default_indentation_increment;
  6079.   close_single_paragraph ();
  6080.  
  6081.   /* Make an entry in the appropriate index. */
  6082.   switch (base_type)
  6083.     {
  6084.     case deffn:
  6085.     case deftypefn:
  6086.       execute_string ("@findex %s\n", defined_name);
  6087.       break;
  6088.     case defvr:
  6089.     case deftypevr:
  6090.     case defcv:
  6091.       execute_string ("@vindex %s\n", defined_name);
  6092.       break;
  6093.     case defop:
  6094.     case deftypemethod:
  6095.       execute_string ("@findex %s on %s\n", defined_name, type_name);
  6096.       break;
  6097.     case deftp:
  6098.       execute_string ("@tindex %s\n", defined_name);
  6099.       break;
  6100.     }
  6101.  
  6102.   /* Deallocate the token list. */
  6103.   scan_args = defun_args;
  6104.   while (1)
  6105.     {
  6106.       char * arg = (*scan_args++);
  6107.       if (arg == NULL)
  6108.     break;
  6109.       free (arg);
  6110.     }
  6111.   free (defun_args);
  6112. }
  6113.  
  6114. /* Add an entry for a function, macro, special form, variable, or option.
  6115.    If the name of the calling command ends in `x', then this is an extra
  6116.    entry included in the body of an insertion of the same type. */
  6117. cm_defun ()
  6118. {
  6119.   int x_p;
  6120.   enum insertion_type type;
  6121.   char *temp = savestring (command);
  6122.  
  6123.   x_p = (command[strlen (command) - 1] == 'x');
  6124.  
  6125.   if (x_p)
  6126.     temp[strlen (temp) - 1] = '\0';
  6127.  
  6128.   type = find_type_from_name (temp);
  6129.   free (temp);
  6130.  
  6131.   /* If we are adding to an already existing insertion, then make sure
  6132.      that we are already in an insertion of type TYPE. */
  6133.   if (x_p &&
  6134.       (!insertion_level || insertion_stack->insertion != type))
  6135.     {
  6136.       line_error ("Must be in a `%s' insertion in order to use `%s'x",
  6137.           command, command);
  6138.       discard_until ("\n");
  6139.       return;
  6140.     }
  6141.  
  6142.   defun_internal (type, x_p);
  6143. }
  6144.  
  6145. /* End existing insertion block. */
  6146. cm_end ()
  6147. {
  6148.   char *temp;
  6149.   enum insertion_type type;
  6150.  
  6151.   if (!insertion_level)
  6152.     {
  6153.       line_error ("Unmatched `@%s'", command);
  6154.       return;
  6155.     }
  6156.  
  6157.   get_rest_of_line (&temp);
  6158.   canon_white (temp);
  6159.  
  6160.   if (strlen (temp) == 0)
  6161.     line_error ("`@%s' needs something after it", command);
  6162.  
  6163.   type = find_type_from_name (temp);
  6164.  
  6165.   if (type == bad_type)
  6166.     {
  6167.       line_error ("Bad argument to `%s', `%s', using `%s'",
  6168.        command, temp, insertion_type_pname (current_insertion_type ()));
  6169.     }
  6170.   end_insertion (type);
  6171.   free (temp);
  6172. }
  6173.  
  6174.  
  6175. /* **************************************************************** */
  6176. /*                                    */
  6177. /*            Other Random Commands                   */
  6178. /*                                    */
  6179. /* **************************************************************** */
  6180.  
  6181. /* This says to inhibit the indentation of the next paragraph, but
  6182.    not of following paragraphs.  */
  6183. cm_noindent ()
  6184. {
  6185.   if (!inhibit_paragraph_indentation)
  6186.     inhibit_paragraph_indentation = -1;
  6187. }
  6188.  
  6189. /* I don't know exactly what to do with this.  Should I allow
  6190.    someone to switch filenames in the middle of output?  Since the
  6191.    file could be partially written, this doesn't seem to make sense.
  6192.    Another option: ignore it, since they don't *really* want to
  6193.    switch files.  Finally, complain, or at least warn. */
  6194. cm_setfilename ()
  6195. {
  6196.   char *filename;
  6197.   get_rest_of_line (&filename);
  6198.   /* warning ("`@%s %s' encountered and ignored", command, filename); */
  6199.   free (filename);
  6200. }
  6201.  
  6202. cm_ignore_line ()
  6203. {
  6204.   discard_until ("\n");
  6205. }
  6206.  
  6207. /* @br can be immediately followed by `{}', so we have to read those here.
  6208.    It should simply close the paragraph. */
  6209. cm_br ()
  6210. {
  6211.   if (looking_at ("{}"))
  6212.     input_text_offset += 2;
  6213.  
  6214.   if (curchar () == '\n')
  6215.     {
  6216.       input_text_offset++;
  6217.       line_number++;
  6218.     }
  6219.  
  6220.   close_paragraph ();
  6221. }
  6222.  
  6223.  /* Insert the number of blank lines passed as argument. */
  6224. cm_sp ()
  6225. {
  6226.   int lines;
  6227.   char *line;
  6228.  
  6229.   get_rest_of_line (&line);
  6230.  
  6231.   if (sscanf (line, "%d", &lines) != 1)
  6232.     {
  6233.       line_error ("%csp requires a positive numeric argument", COMMAND_PREFIX);
  6234.     }
  6235.   else
  6236.     {
  6237.       if (lines < 0)
  6238.     lines = 0;
  6239.  
  6240.       while (lines--)
  6241.     add_char ('\n');
  6242.     }
  6243.   free (line);
  6244. }
  6245.  
  6246. /* Start a new line with just this text on it.
  6247.    Then center the line of text.
  6248.    This always ends the current paragraph. */
  6249. cm_center ()
  6250. {
  6251.   register int i, start, length;
  6252.   int fudge_factor = 1;
  6253.   unsigned char *line;
  6254.  
  6255.   close_paragraph ();
  6256.   filling_enabled = indented_fill = 0;
  6257.   cm_noindent ();
  6258.   start = output_paragraph_offset;
  6259.   inhibit_output_flushing ();
  6260.   get_rest_of_line ((char **)&line);
  6261.   execute_string ((char *)line);
  6262.   free (line);
  6263.   uninhibit_output_flushing ();
  6264.  
  6265.   i = output_paragraph_offset - 1;
  6266.   while (i > (start - 1) && output_paragraph[i] == '\n')
  6267.     i--;
  6268.  
  6269.   output_paragraph_offset = ++i;
  6270.   length = output_paragraph_offset - start;
  6271.  
  6272.   if (length < (fill_column - fudge_factor))
  6273.     {
  6274.       line = (unsigned char *)xmalloc (1 + length);
  6275.       memcpy (line, (char *)(output_paragraph + start), length);
  6276.  
  6277.       i = (fill_column - fudge_factor - length) / 2;
  6278.       output_paragraph_offset = start;
  6279.  
  6280.       while (i--)
  6281.     insert (' ');
  6282.  
  6283.       for (i = 0; i < length; i++)
  6284.     insert (line[i]);
  6285.  
  6286.       free (line);
  6287.     }
  6288.  
  6289.   insert ('\n');
  6290.   close_paragraph ();
  6291.   filling_enabled = 1;
  6292. }
  6293.  
  6294. /* Show what an expression returns. */
  6295. cm_result (arg)
  6296.      int arg;
  6297. {
  6298.   if (arg == END)
  6299.     add_word ("=>");
  6300. }
  6301.  
  6302. /* What an expression expands to. */
  6303. cm_expansion (arg)
  6304.      int arg;
  6305. {
  6306.   if (arg == END)
  6307.     add_word ("==>");
  6308. }
  6309.  
  6310. /* Indicates two expressions are equivalent. */
  6311. cm_equiv (arg)
  6312.      int arg;
  6313. {
  6314.   if (arg == END)
  6315.     add_word ("==");
  6316. }
  6317.  
  6318. /* What an expression may print. */
  6319. cm_print (arg)
  6320.      int arg;
  6321. {
  6322.   if (arg == END)
  6323.     add_word ("-|");
  6324. }
  6325.  
  6326. /* An error signaled. */
  6327. cm_error (arg)
  6328.      int arg;
  6329. {
  6330.   if (arg == END)
  6331.     add_word ("error-->");
  6332. }
  6333.  
  6334. /* The location of point in an example of a buffer. */
  6335. cm_point (arg)
  6336.      int arg;
  6337. {
  6338.   if (arg == END)
  6339.     add_word ("-!-");
  6340. }
  6341.  
  6342. /* Start a new line with just this text on it.
  6343.    The text is outdented one level if possible. */
  6344. cm_exdent ()
  6345. {
  6346.   char *line;
  6347.   int i = current_indent;
  6348.  
  6349.   if (current_indent)
  6350.     current_indent -= default_indentation_increment;
  6351.  
  6352.   get_rest_of_line (&line);
  6353.   close_single_paragraph ();
  6354.   execute_string ("%s", line);
  6355.   current_indent = i;
  6356.   free (line);
  6357.   close_single_paragraph ();
  6358. }
  6359.  
  6360. cm_include ()
  6361. {
  6362.   cm_infoinclude ();
  6363. }
  6364.  
  6365. /* Remember this file, and move onto the next. */
  6366. cm_infoinclude ()
  6367. {
  6368.   char *filename;
  6369.  
  6370.   close_paragraph ();
  6371.   get_rest_of_line (&filename);
  6372.   pushfile ();
  6373.  
  6374.   /* In verbose mode we print info about including another file. */
  6375.   if (verbose_mode)
  6376.     {
  6377.       register int i = 0;
  6378.       register FSTACK *stack = filestack;
  6379.  
  6380.       for (i = 0, stack = filestack; stack; stack = stack->next, i++);
  6381.  
  6382.       i *= 2;
  6383.  
  6384.       printf ("%*s", i, "");
  6385.       printf ("%c%s %s\n", COMMAND_PREFIX, command, filename);
  6386.       fflush (stdout);
  6387.     }
  6388.  
  6389.   if (!find_and_load (filename))
  6390.     {
  6391.       extern char *sys_errlist[];
  6392.       extern int errno, sys_nerr;
  6393.       popfile ();
  6394.  
  6395.       /* Cannot "@include foo", in line 5 of "/wh/bar". */
  6396.       line_error ("`%c%s %s': %s", COMMAND_PREFIX, command, filename,
  6397.           ((errno < sys_nerr) ?
  6398.            sys_errlist[errno] : "Unknown file system error"));
  6399.     }
  6400.   free (filename);
  6401. }
  6402.  
  6403. /* The other side of a malformed expression. */
  6404. misplaced_brace ()
  6405. {
  6406.   line_error ("Misplaced `}'");
  6407. }
  6408.  
  6409. /* Don't let the filling algorithm insert extra whitespace here. */
  6410. cm_force_abbreviated_whitespace ()
  6411. {
  6412. }
  6413.  
  6414. /* Do not let this character signify the end of a sentence, though
  6415.    if it was seen without the command prefix it normally would.  We
  6416.    do this by turning on the 8th bit of the character. */
  6417. cm_ignore_sentence_ender ()
  6418. {
  6419.   add_char (META ((*command)));
  6420. }
  6421.  
  6422. /* Signals end of processing.  Easy to make this happen. */
  6423. cm_bye ()
  6424. {
  6425.   input_text_offset = size_of_input_text;
  6426. }
  6427.  
  6428. cm_asis ()
  6429. {
  6430. }
  6431.  
  6432. cm_math ()
  6433. {
  6434. }
  6435.  
  6436.  
  6437. /* **************************************************************** */
  6438. /*                                    */
  6439. /*            Indexing Stuff                    */
  6440. /*                                    */
  6441. /* **************************************************************** */
  6442.  
  6443.  
  6444. /* An index element... */
  6445. typedef struct index_elt
  6446. {
  6447.   struct index_elt *next;
  6448.   char *entry;            /* The index entry itself. */
  6449.   char *node;            /* The node from whence it came. */
  6450.   int code;            /* Non-zero means add `@code{...}' when
  6451.                    printing this element. */
  6452.   int defining_line;        /* Line number where this entry was written. */
  6453. } INDEX_ELT;
  6454.  
  6455. /* A list of short-names for each index, and the index to that index in our
  6456.    index array, the_indices.  In addition, for each index, it is remembered
  6457.    whether that index is a code index or not.  Code indices have @code{}
  6458.    inserted around the first word when they are printed with printindex. */
  6459. typedef struct
  6460. {
  6461.   char *name;
  6462.   int index;
  6463.   int code;
  6464. } INDEX_ALIST;
  6465.  
  6466. INDEX_ALIST **name_index_alist = (INDEX_ALIST **) NULL;
  6467.  
  6468. /* An array of pointers.  Each one is for a different index.  The
  6469.    "synindex" command changes which array slot is pointed to by a
  6470.    given "index". */
  6471. INDEX_ELT **the_indices = (INDEX_ELT **) NULL;
  6472.  
  6473. /* The number of defined indices. */
  6474. int defined_indices = 0;
  6475.  
  6476. /* We predefine these. */
  6477. #define program_index 0
  6478. #define function_index 1
  6479. #define concept_index 2
  6480. #define variable_index 3
  6481. #define datatype_index 4
  6482. #define key_index 5
  6483.  
  6484. init_indices ()
  6485. {
  6486.   int i;
  6487.  
  6488.   /* Create the default data structures. */
  6489.  
  6490.   /* Initialize data space. */
  6491.   if (!the_indices)
  6492.     {
  6493.       the_indices = (INDEX_ELT **) xmalloc ((1 + defined_indices) *
  6494.                         sizeof (INDEX_ELT *));
  6495.       the_indices[defined_indices] = (INDEX_ELT *) NULL;
  6496.  
  6497.       name_index_alist = (INDEX_ALIST **) xmalloc ((1 + defined_indices) *
  6498.                            sizeof (INDEX_ALIST *));
  6499.       name_index_alist[defined_indices] = (INDEX_ALIST *) NULL;
  6500.     }
  6501.  
  6502.   /* If there were existing indices, get rid of them now. */
  6503.   for (i = 0; i < defined_indices; i++)
  6504.     undefindex (name_index_alist[i]->name);
  6505.  
  6506.   /* Add the default indices. */
  6507.   defindex ("pg", 0);
  6508.   defindex ("fn", 1);        /* "fn" is a code index.  */
  6509.   defindex ("cp", 0);
  6510.   defindex ("vr", 0);
  6511.   defindex ("tp", 0);
  6512.   defindex ("ky", 0);
  6513.  
  6514. }
  6515.  
  6516. /* Find which element in the known list of indices has this name.
  6517.    Returns -1 if NAME isn't found. */
  6518. int
  6519. find_index_offset (name)
  6520.      char *name;
  6521. {
  6522.   register int i;
  6523.   for (i = 0; i < defined_indices; i++)
  6524.     if (name_index_alist[i] &&
  6525.     strcmp (name, name_index_alist[i]->name) == 0)
  6526.       return (name_index_alist[i]->index);
  6527.   return (-1);
  6528. }
  6529.  
  6530. /* Return a pointer to the entry of (name . index) for this name.
  6531.    Return NULL if the index doesn't exist. */
  6532. INDEX_ALIST *
  6533. find_index (name)
  6534.      char *name;
  6535. {
  6536.   int offset = find_index_offset (name);
  6537.   if (offset > -1)
  6538.     return (name_index_alist[offset]);
  6539.   else
  6540.     return ((INDEX_ALIST *) NULL);
  6541. }
  6542.  
  6543. /* Given an index name, return the offset in the_indices of this index,
  6544.    or -1 if there is no such index. */
  6545. translate_index (name)
  6546.      char *name;
  6547. {
  6548.   INDEX_ALIST *which = find_index (name);
  6549.  
  6550.   if (which)
  6551.     return (which->index);
  6552.   else
  6553.     return (-1);
  6554. }
  6555.  
  6556. /* Return the index list which belongs to NAME. */
  6557. INDEX_ELT *
  6558. index_list (name)
  6559.      char *name;
  6560. {
  6561.   int which = translate_index (name);
  6562.   if (which < 0)
  6563.     return ((INDEX_ELT *) - 1);
  6564.   else
  6565.     return (the_indices[which]);
  6566. }
  6567.  
  6568. /* Please release me, let me go... */
  6569. free_index (index)
  6570.      INDEX_ELT *index;
  6571. {
  6572.   INDEX_ELT *temp;
  6573.  
  6574.   while ((temp = index) != (INDEX_ELT *) NULL)
  6575.     {
  6576.       free (temp->entry);
  6577.       free (temp->node);
  6578.       index = index->next;
  6579.       free (temp);
  6580.     }
  6581. }
  6582.  
  6583. /* Flush an index by name. */
  6584. undefindex (name)
  6585.      char *name;
  6586. {
  6587.   int i;
  6588.   int which = find_index_offset (name);
  6589.  
  6590.   if (which < 0)
  6591.     return (which);
  6592.  
  6593.   i = name_index_alist[which]->index;
  6594.  
  6595.  
  6596.   free_index (the_indices[i]);
  6597.   the_indices[i] = (INDEX_ELT *) NULL;
  6598.  
  6599.   free (name_index_alist[which]->name);
  6600.   free (name_index_alist[which]);
  6601.   name_index_alist[which] = (INDEX_ALIST *) NULL;
  6602. }
  6603.  
  6604. /* Define an index known as NAME.  We assign the slot number.
  6605.    CODE if non-zero says to make this a code index. */
  6606. defindex (name, code)
  6607.      char *name;
  6608.      int code;
  6609. {
  6610.   register int i, slot;
  6611.  
  6612.   /* If it already exists, flush it. */
  6613.   undefindex (name);
  6614.  
  6615.   /* Try to find an empty slot. */
  6616.   slot = -1;
  6617.   for (i = 0; i < defined_indices; i++)
  6618.     if (!name_index_alist[i])
  6619.       {
  6620.     slot = i;
  6621.     break;
  6622.       }
  6623.  
  6624.   if (slot < 0)
  6625.     {
  6626.       /* No such luck.  Make space for another index. */
  6627.       slot = defined_indices;
  6628.       defined_indices++;
  6629.  
  6630.       name_index_alist = (INDEX_ALIST **)
  6631.     xrealloc ((char *)name_index_alist,
  6632.           (1 + defined_indices) * sizeof (INDEX_ALIST *));
  6633.       the_indices = (INDEX_ELT **)
  6634.     xrealloc ((char *)the_indices,
  6635.           (1 + defined_indices) * sizeof (INDEX_ELT *));
  6636.     }
  6637.  
  6638.   /* We have a slot.  Start assigning. */
  6639.   name_index_alist[slot] = (INDEX_ALIST *) xmalloc (sizeof (INDEX_ALIST));
  6640.   name_index_alist[slot]->name = savestring (name);
  6641.   name_index_alist[slot]->index = slot;
  6642.   name_index_alist[slot]->code = code;
  6643.  
  6644.   the_indices[slot] = (INDEX_ELT *) NULL;
  6645. }
  6646.  
  6647. /* Add the arguments to the current index command to the index NAME. */
  6648. index_add_arg (name)
  6649.      char *name;
  6650. {
  6651.   int which;
  6652.   char *index_entry;
  6653.   INDEX_ALIST *tem;
  6654.  
  6655.   tem = find_index (name);
  6656.  
  6657.   which = tem ? tem->index : -1;
  6658.  
  6659.   get_rest_of_line (&index_entry);
  6660.   ignore_blank_line ();
  6661.  
  6662.   if (which < 0)
  6663.     {
  6664.       line_error ("Unknown index reference `%s'", name);
  6665.       free (index_entry);
  6666.     }
  6667.   else
  6668.     {
  6669.       INDEX_ELT *new = (INDEX_ELT *) xmalloc (sizeof (INDEX_ELT));
  6670.       new->next = the_indices[which];
  6671.       new->entry = index_entry;
  6672.       new->node = current_node;
  6673.       new->code = tem->code;
  6674.       new->defining_line = line_number - 1;
  6675.       the_indices[which] = new;
  6676.     }
  6677. }
  6678.  
  6679. #define INDEX_COMMAND_SUFFIX "index"
  6680.  
  6681. /* The function which user defined index commands call. */
  6682. gen_index ()
  6683. {
  6684.   char *name = savestring (command);
  6685.   if (strlen (name) >= strlen ("index"))
  6686.     name[strlen (name) - strlen ("index")] = '\0';
  6687.   index_add_arg (name);
  6688.   free (name);
  6689. }
  6690.  
  6691. /* Define a new index command.  Arg is name of index. */
  6692. cm_defindex ()
  6693. {
  6694.   gen_defindex (0);
  6695. }
  6696.  
  6697. cm_defcodeindex ()
  6698. {
  6699.   gen_defindex (1);
  6700. }
  6701.  
  6702. gen_defindex (code)
  6703.      int code;
  6704. {
  6705.   char *name;
  6706.   get_rest_of_line (&name);
  6707.  
  6708.   if (find_index (name))
  6709.     {
  6710.       line_error ("Index `%s' already exists", name);
  6711.       free (name);
  6712.       return;
  6713.     }
  6714.   else
  6715.     {
  6716.       char *temp = (char *) alloca (1 + strlen (name) + strlen ("index"));
  6717.       sprintf (temp, "%sindex", name);
  6718.       define_user_command (temp, gen_index, 0);
  6719.       defindex (name, code);
  6720.       free (name);
  6721.     }
  6722. }
  6723.  
  6724. /* Append LIST2 to LIST1.  Return the head of the list. */
  6725. INDEX_ELT *
  6726. index_append (head, tail)
  6727.      INDEX_ELT *head, *tail;
  6728. {
  6729.   register INDEX_ELT *t_head = head;
  6730.  
  6731.   if (!t_head)
  6732.     return (tail);
  6733.  
  6734.   while (t_head->next)
  6735.     t_head = t_head->next;
  6736.   t_head->next = tail;
  6737.   return (head);
  6738. }
  6739.  
  6740. /* Expects 2 args, on the same line.  Both are index abbreviations.
  6741.    Make the first one be a synonym for the second one, i.e. make the
  6742.    first one have the same index as the second one. */
  6743. cm_synindex ()
  6744. {
  6745.   int redirector, redirectee;
  6746.   char *temp;
  6747.  
  6748.   skip_whitespace ();
  6749.   get_until_in_line (" ", &temp);
  6750.   redirectee = find_index_offset (temp);
  6751.   skip_whitespace ();
  6752.   free_and_clear (&temp);
  6753.   get_until_in_line (" ", &temp);
  6754.   redirector = find_index_offset (temp);
  6755.   free (temp);
  6756.   if (redirector < 0 || redirectee < 0)
  6757.     {
  6758.       line_error ("Unknown index reference");
  6759.     }
  6760.   else
  6761.     {
  6762.       /* I think that we should let the user make indices synonymous to
  6763.          each other without any lossage of info.  This means that one can
  6764.          say @synindex cp dt anywhere in the file, and things that used to
  6765.          be in cp will go into dt. */
  6766.       INDEX_ELT *i1 = the_indices[redirectee], *i2 = the_indices[redirector];
  6767.  
  6768.       if (i1 || i2)
  6769.     {
  6770.       if (i1)
  6771.         the_indices[redirectee] = index_append (i1, i2);
  6772.       else
  6773.         the_indices[redirectee] = index_append (i2, i1);
  6774.     }
  6775.  
  6776.       name_index_alist[redirectee]->index =
  6777.     name_index_alist[redirector]->index;
  6778.     }
  6779. }
  6780.  
  6781. cm_pindex ()            /* Pinhead index. */
  6782. {
  6783.   index_add_arg ("pg");
  6784. }
  6785.  
  6786. cm_vindex ()            /* Variable index. */
  6787. {
  6788.   index_add_arg ("vr");
  6789. }
  6790.  
  6791. cm_kindex ()            /* Key index. */
  6792. {
  6793.   index_add_arg ("ky");
  6794. }
  6795.  
  6796. cm_cindex ()            /* Concept index. */
  6797. {
  6798.   index_add_arg ("cp");
  6799. }
  6800.  
  6801. cm_findex ()            /* Function index. */
  6802. {
  6803.   index_add_arg ("fn");
  6804. }
  6805.  
  6806. cm_tindex ()            /* Data Type index. */
  6807. {
  6808.   index_add_arg ("tp");
  6809. }
  6810.  
  6811. /* Sorting the index. */
  6812. index_element_compare (element1, element2)
  6813.      INDEX_ELT **element1, **element2;
  6814. {
  6815.   /* This needs to ignore leading non-text characters. */
  6816.   return (stricmp ((*element1)->entry, (*element2)->entry));
  6817. }
  6818.  
  6819. /* Sort the index passed in INDEX, returning an array of
  6820.    pointers to elements.  The array is terminated with a NULL
  6821.    pointer.  We call qsort because it's supposed to be fast.
  6822.    I think this looks bad. */
  6823. INDEX_ELT **
  6824. sort_index (index)
  6825.      INDEX_ELT *index;
  6826. {
  6827.   INDEX_ELT *temp = index;
  6828.   INDEX_ELT **array;
  6829.   int count = 0;
  6830.  
  6831.   while (temp != (INDEX_ELT *) NULL)
  6832.     {
  6833.       count++;
  6834.       temp = temp->next;
  6835.     }
  6836.  
  6837.   /* We have the length.  Make an array. */
  6838.  
  6839.   array = (INDEX_ELT **) xmalloc ((count + 1) * sizeof (INDEX_ELT *));
  6840.   count = 0;
  6841.   temp = index;
  6842.  
  6843.   while (temp != (INDEX_ELT *) NULL)
  6844.     {
  6845.       array[count++] = temp;
  6846.       temp = temp->next;
  6847.     }
  6848.   array[count] = (INDEX_ELT *) NULL;    /* terminate the array. */
  6849.  
  6850.   /* Sort the array. */
  6851.   qsort (array, count, sizeof (INDEX_ELT *), index_element_compare);
  6852.  
  6853.   return (array);
  6854. }
  6855.  
  6856. /* Non-zero means that we are in the middle of printing an index. */
  6857. int printing_index = 0;
  6858.  
  6859. /* Takes one arg, a short name of an index to print.
  6860.    Outputs a menu of the sorted elements of the index. */
  6861. cm_printindex ()
  6862. {
  6863.   int item;
  6864.   INDEX_ELT *index;
  6865.   INDEX_ELT **array;
  6866.   char *index_name;
  6867.   int old_inhibitions = inhibit_paragraph_indentation;
  6868.   int previous_filling_enabled_value = filling_enabled;
  6869.  
  6870.   close_paragraph ();
  6871.   get_rest_of_line (&index_name);
  6872.  
  6873.   index = index_list (index_name);
  6874.   if ((int) index == -1)
  6875.     {
  6876.       line_error ("Unknown index name `%s'", index_name);
  6877.       free (index_name);
  6878.       return;
  6879.     }
  6880.   else
  6881.     free (index_name);
  6882.  
  6883.   array = sort_index (index);
  6884.  
  6885.   filling_enabled = 0;
  6886.   inhibit_paragraph_indentation = 1;
  6887.   close_paragraph ();
  6888.   add_word ("* Menu:\n\n");
  6889.  
  6890.   printing_index = 1;
  6891.   for (item = 0; (index = array[item]); item++)
  6892.     {
  6893.       int real_line_number = line_number;
  6894.  
  6895.       /* Let errors generated while making the index entry point back
  6896.      at the line which contains the entry. */
  6897.       line_number = index->defining_line;
  6898.  
  6899.       /* If this particular entry should be printed as a "code" index,
  6900.      then wrap the entry with "@code{...}". */
  6901.       if (index->code)
  6902.     execute_string ("* @code{%s}: ", index->entry);
  6903.       else
  6904.     execute_string ("* %s: ", index->entry);
  6905.  
  6906.       /* Pad the front of the destination nodename so that
  6907.      the output looks nice. */
  6908.       if (fill_column > 40 && output_column < 40)
  6909.     indent (40 - output_column);
  6910.  
  6911.       execute_string ("%s.\n", index->node);
  6912.  
  6913.       line_number = real_line_number;
  6914.       flush_output ();
  6915.     }
  6916.  
  6917.   printing_index = 0;
  6918.   free (array);
  6919.   close_single_paragraph ();
  6920.   filling_enabled = previous_filling_enabled_value;
  6921.   inhibit_paragraph_indentation = old_inhibitions;
  6922. }
  6923.  
  6924.  
  6925. /* **************************************************************** */
  6926. /*                                    */
  6927. /*            Making User Defined Commands            */
  6928. /*                                    */
  6929. /* **************************************************************** */
  6930.  
  6931. define_user_command (name, proc, needs_braces_p)
  6932.      char *name;
  6933.      FUNCTION *proc;
  6934.      int needs_braces_p;
  6935. {
  6936.   int slot = user_command_array_len;
  6937.   user_command_array_len++;
  6938.  
  6939.   if (!user_command_array)
  6940.     user_command_array = (COMMAND **) xmalloc (1 * sizeof (COMMAND *));
  6941.  
  6942.   user_command_array = (COMMAND **) xrealloc (user_command_array,
  6943.                           (1 + user_command_array_len) *
  6944.                           sizeof (COMMAND *));
  6945.  
  6946.   user_command_array[slot] = (COMMAND *) xmalloc (sizeof (COMMAND));
  6947.   user_command_array[slot]->name = savestring (name);
  6948.   user_command_array[slot]->proc = proc;
  6949.   user_command_array[slot]->argument_in_braces = needs_braces_p;
  6950. }
  6951.  
  6952. /* Make ALIAS run the named FUNCTION.  Copies properties from FUNCTION. */
  6953. define_alias (alias, function)
  6954.      char *alias, *function;
  6955. {
  6956. }
  6957.  
  6958. /* Set the paragraph indentation variable to the value specified in STRING.
  6959.    Values can be:
  6960.    `asis': Don't change existing indentation.
  6961.    `none': Remove existing indentation.
  6962.       NUM: Indent NUM spaces at the starts of paragraphs.
  6963.            Note that if NUM is zero, we assume `none'.
  6964.  
  6965.    Returns 0 if successful, or non-zero if STRING isn't one of the above. */
  6966. int
  6967. set_paragraph_indent (string)
  6968.      char *string;
  6969. {
  6970.   if (strcmp (string, "asis") == 0)
  6971.     paragraph_start_indent = 0;
  6972.   else if (strcmp (string, "none") == 0)
  6973.     paragraph_start_indent = -1;
  6974.   else
  6975.     {
  6976.       if (sscanf (string, "%d", ¶graph_start_indent) != 1)
  6977.     return (-1);
  6978.       else
  6979.     {
  6980.       if (paragraph_start_indent == 0)
  6981.         paragraph_start_indent = -1;
  6982.     }
  6983.     }
  6984.   return (0);
  6985. }
  6986.  
  6987. cm_paragraphindent ()
  6988. {
  6989.   char *arg;
  6990.  
  6991.   get_rest_of_line (&arg);
  6992.   if (set_paragraph_indent (arg) != 0)
  6993.     line_error ("Bad argument to @paragraphindent");
  6994.  
  6995.   free (arg);
  6996. }
  6997.  
  6998. /* Some support for footnotes. */
  6999.  
  7000. /* Footnotes are a new construct in Info.  We don't know the best method
  7001.    of implementing them for sure, so we present two possiblities.
  7002.  
  7003.    SeparateNode:
  7004.     Make them look like followed references, with the reference
  7005.     destinations in a makeinfo manufactured node or,
  7006.  
  7007.    EndNode:
  7008.     Make them appear at the bottom of the node that they originally
  7009.     appeared in. */
  7010. #define SeparateNode 0
  7011. #define EndNode 1
  7012.  
  7013. int footnote_style = EndNode;
  7014. int first_footnote_this_node = 1;
  7015. int footnote_count = 0;
  7016.  
  7017. /* Set the footnote style based on he style identifier in STRING. */
  7018. int
  7019. set_footnote_style (string)
  7020.      char *string;
  7021. {
  7022.   if ((stricmp (string, "separate") == 0) ||
  7023.       (stricmp (string, "MN") == 0))
  7024.     footnote_style = SeparateNode;
  7025.   else if ((stricmp (string, "end") == 0) ||
  7026.        (stricmp (string, "EN") == 0))
  7027.     footnote_style = EndNode;
  7028.   else
  7029.     return (-1);
  7030.  
  7031.  return (0);
  7032. }
  7033.  
  7034. cm_footnotestyle ()
  7035. {
  7036.   char *arg;
  7037.  
  7038.   get_rest_of_line (&arg);
  7039.  
  7040.   if (set_footnote_style (arg) != 0)
  7041.     line_error ("Bad argument to @footnotestyle");
  7042.  
  7043.   free (arg);
  7044. }
  7045.  
  7046. typedef struct fn
  7047. {
  7048.   struct fn *next;
  7049.   char *marker;
  7050.   char *note;
  7051. }  FN;
  7052.  
  7053. FN *pending_notes = (FN *) NULL;
  7054.  
  7055. /* A method for remembering footnotes.  Note that this list gets output
  7056.    at the end of the current node. */
  7057. remember_note (marker, note)
  7058.      char *marker, *note;
  7059. {
  7060.   FN *temp = (FN *) xmalloc (sizeof (FN));
  7061.  
  7062.   temp->marker = savestring (marker);
  7063.   temp->note = savestring (note);
  7064.   temp->next = pending_notes;
  7065.   pending_notes = temp;
  7066.   footnote_count++;
  7067. }
  7068.  
  7069. /* How to get rid of existing footnotes. */
  7070. free_pending_notes ()
  7071. {
  7072.   FN *temp;
  7073.  
  7074.   while ((temp = pending_notes) != (FN *) NULL)
  7075.     {
  7076.       free (temp->marker);
  7077.       free (temp->note);
  7078.       pending_notes = pending_notes->next;
  7079.       free (temp);
  7080.     }
  7081.   first_footnote_this_node = 1;
  7082.   footnote_count = 0;
  7083. }
  7084.  
  7085. /* What to do when you see a @footnote construct. */
  7086.  
  7087.  /* Handle a "footnote".
  7088.     footnote *{this is a footnote}
  7089.     where "*" is the marker character for this note. */
  7090. cm_footnote ()
  7091. {
  7092.   char *marker;
  7093.   char *note;
  7094.  
  7095.   get_until ("{", &marker);
  7096.   canon_white (marker);
  7097.  
  7098.   /* Read the argument in braces. */
  7099.   if (curchar () != '{')
  7100.     {
  7101.       line_error ("`@%s' expected more than just `%s'.  It needs something in `{...}'", command, marker);
  7102.       free (marker);
  7103.       return;
  7104.     }
  7105.   else
  7106.     {
  7107.       int braces = 1;
  7108.       int temp = ++input_text_offset;
  7109.       int len;
  7110.  
  7111.       while (braces)
  7112.     {
  7113.       if (temp == size_of_input_text)
  7114.         {
  7115.           line_error ("No closing brace for footnote `%s'", marker);
  7116.           return;
  7117.         }
  7118.  
  7119.       if (input_text[temp] == '{')
  7120.         braces++;
  7121.       else if (input_text[temp] == '}')
  7122.         braces--;
  7123.       else if (input_text[temp] == '\n')
  7124.         line_number ++;
  7125.  
  7126.       temp++;
  7127.     }
  7128.  
  7129.       len = (temp - input_text_offset) - 1;
  7130.       note = (char *)xmalloc (len + 1);
  7131.       strncpy (note, &input_text[input_text_offset], len);
  7132.       note[len] = '\0';
  7133.       input_text_offset = temp;
  7134.     }
  7135.  
  7136.   if (!current_node || !*current_node)
  7137.     {
  7138.       line_error ("Footnote defined without parent node");
  7139.       free (marker);
  7140.       free (note);
  7141.       return;
  7142.     }
  7143.  
  7144.   if (!*marker)
  7145.     {
  7146.       free (marker);
  7147.  
  7148.       if (number_footnotes)
  7149.     {
  7150.       marker = (char *)xmalloc (10);
  7151.       sprintf (marker, "%d", current_footnote_number);
  7152.       current_footnote_number++;
  7153.     }
  7154.       else
  7155.     marker = savestring ("*");
  7156.     }
  7157.  
  7158.   remember_note (marker, note);
  7159.  
  7160.   /* Your method should at least insert MARKER. */
  7161.   switch (footnote_style)
  7162.     {
  7163.     case SeparateNode:
  7164.       add_word_args ("(%s)", marker);
  7165.       if (first_footnote_this_node)
  7166.     {
  7167.       char *temp_string;
  7168.  
  7169.       temp_string = (char *)
  7170.         xmalloc ((strlen (current_node)) + (strlen ("-Footnotes")) + 1);
  7171.  
  7172.       add_word_args (" (*note %s-Footnotes::)", current_node);
  7173.       strcpy (temp_string, current_node);
  7174.       strcat (temp_string, "-Footnotes");
  7175.       remember_node_reference (temp_string, line_number, followed_reference);
  7176.       free (temp_string);
  7177.       first_footnote_this_node = 0;
  7178.     }
  7179.       break;
  7180.  
  7181.     case EndNode:
  7182.       add_word_args ("(%s)", marker);
  7183.       break;
  7184.  
  7185.     default:
  7186.       break;
  7187.     }
  7188.   free (marker);
  7189.   free (note);
  7190. }
  7191.  
  7192. /* Non-zero means that we are currently in the process of outputting
  7193.    footnotes. */
  7194. int already_outputting_pending_notes = 0;
  7195.  
  7196. /* Output the footnotes.  We are at the end of the current node. */
  7197. output_pending_notes ()
  7198. {
  7199.   FN *footnote = pending_notes;
  7200.  
  7201.   if (!pending_notes)
  7202.     return;
  7203.  
  7204.   switch (footnote_style)
  7205.     {
  7206.  
  7207.     case SeparateNode:
  7208.       {
  7209.     char *old_current_node = current_node;
  7210.     char *old_command = savestring (command);
  7211.  
  7212.     already_outputting_pending_notes++;
  7213.     execute_string ("@node %s-Footnotes,,,%s\n", current_node, current_node);
  7214.     already_outputting_pending_notes--;
  7215.     current_node = old_current_node;
  7216.     free (command);
  7217.     command = old_command;
  7218.       }
  7219.       break;
  7220.  
  7221.     case EndNode:
  7222.       close_paragraph ();
  7223.       in_fixed_width_font++;
  7224.       execute_string ("---------- Footnotes ----------\n\n");
  7225.       in_fixed_width_font--;
  7226.       break;
  7227.     }
  7228.  
  7229.   /* Handle the footnotes in reverse order. */
  7230.   {
  7231.     FN **array = (FN **) xmalloc ((footnote_count + 1) * sizeof (FN *));
  7232.  
  7233.     array[footnote_count] = (FN *) NULL;
  7234.  
  7235.     while (--footnote_count > -1)
  7236.       {
  7237.     array[footnote_count] = footnote;
  7238.     footnote = footnote->next;
  7239.       }
  7240.  
  7241.     filling_enabled = 1;
  7242.     indented_fill = 1;
  7243.  
  7244.     while (footnote = array[++footnote_count])
  7245.       {
  7246.  
  7247.     switch (footnote_style)
  7248.       {
  7249.       case SeparateNode:
  7250.       case EndNode:
  7251.         execute_string ("(%s)  %s", footnote->marker, footnote->note);
  7252.         close_paragraph ();
  7253.         break;
  7254.       }
  7255.       }
  7256.     close_paragraph ();
  7257.     free (array);
  7258.   }
  7259. }
  7260.  
  7261.  
  7262. /* **************************************************************** */
  7263. /*                                                                  */
  7264. /*              User definable Macros (text substitution)        */
  7265. /*                                                                  */
  7266. /* **************************************************************** */
  7267.  
  7268. #if defined (HAVE_MACROS)
  7269.  
  7270. /* Array of macros and definitions. */
  7271. MACRO_DEF **macro_list = (MACRO_DEF **)NULL;
  7272.  
  7273. int macro_list_len = 0;        /* Number of elements. */
  7274. int macro_list_size = 0;    /* Number of slots in total. */
  7275.  
  7276. /* Return the macro definition of NAME or NULL if NAME is not defined. */
  7277. MACRO_DEF *
  7278. find_macro (name)
  7279.      char *name;
  7280. {
  7281.   register int i;
  7282.   register MACRO_DEF *def;
  7283.  
  7284.   def = (MACRO_DEF *)NULL;
  7285.   for (i = 0; macro_list && (def = macro_list[i]); i++)
  7286.     if (strcmp (def->name, name) == 0)
  7287.       break;
  7288.  
  7289.   return (def);
  7290. }
  7291.  
  7292. /* Add the macro NAME with DEFINITION to macro_list.  FILENAME is
  7293.    the name of the file where this definition can be found, and
  7294.    LINENO is the line number within that file.  If a macro already
  7295.    exists with NAME, then a warning is produced, and that previous
  7296.    definition is overwritten. */
  7297. void
  7298. add_macro (name, definition, filename, lineno)
  7299.      char *name, *definition;
  7300.      char *filename;
  7301.      int lineno;
  7302. {
  7303.   register MACRO_DEF *def;
  7304.  
  7305.   def = find_macro (name);
  7306.  
  7307.   if (!def)
  7308.     {
  7309.       if (macro_list_len + 2 >= macro_list_size)
  7310.     macro_list = (MACRO_DEF **)xrealloc
  7311.       (macro_list, ((macro_list_size += 10) * sizeof (MACRO_DEF *)));
  7312.  
  7313.       macro_list[macro_list_len] = (MACRO_DEF *)xmalloc (sizeof (MACRO_DEF));
  7314.       macro_list[macro_list_len + 1] = (MACRO_DEF *)NULL;
  7315.  
  7316.       def = macro_list[macro_list_len];
  7317.       macro_list_len += 1;
  7318.       def->name = savestring (name);
  7319.     }
  7320.   else
  7321.     {
  7322.       char *temp_filename = input_filename;
  7323.       int temp_line = line_number;
  7324.  
  7325.       warning ("The macro `%s' is previously defined.", name);
  7326.  
  7327.       input_filename = def->filename;
  7328.       line_number = def->lineno;
  7329.  
  7330.       warning ("Here is the previous definition of `%s'.", name);
  7331.  
  7332.       input_filename = temp_filename;
  7333.       line_number = temp_line;
  7334.  
  7335.       free (def->filename);
  7336.       free (def->definition);
  7337.     }
  7338.  
  7339.   def->filename = savestring (filename);
  7340.   def->lineno = lineno;
  7341.   def->definition = savestring (definition);
  7342. }
  7343.  
  7344.  
  7345. /* Delete the macro with name NAME.  The macro is deleted from the list,
  7346.    but it is also returned.  If there was no macro defined, NULL is
  7347.    returned. */
  7348. MACRO_DEF *
  7349. delete_macro (name)
  7350.      char *name;
  7351. {
  7352.   register int i;
  7353.   register MACRO_DEF *def;
  7354.  
  7355.   def = (MACRO_DEF *)NULL;
  7356.   for (i = 0; macro_list && (def = macro_list[i]); i++)
  7357.     if (strcmp (def->name, name) == 0)
  7358.       {
  7359.     memcpy (macro_list + i, macro_list + i + 1,
  7360.            ((macro_list_len + 1) - i) * sizeof (MACRO_DEF *));
  7361.     break;
  7362.       }
  7363.   return (def);
  7364. }
  7365.  
  7366. /* Execute the macro passed in DEF, a pointer to a MACRO_DEF. */
  7367. void
  7368. execute_macro (def)
  7369.      MACRO_DEF *def;
  7370. {
  7371.  
  7372.   if (def != (MACRO_DEF *)NULL)
  7373.     {
  7374.       char *line, *string;
  7375.  
  7376.       get_until ("\n", &line);
  7377.  
  7378.       if (curchar () == '\n')    /* as opposed to the end of the file... */
  7379.     {
  7380.       line_number++;
  7381.       input_text_offset++;
  7382.     }
  7383.       string = (char *)xmalloc (1 + strlen (def->definition) + strlen (line));
  7384.       strcpy (string, def->definition);
  7385.       strcat (string, line);
  7386.       free (line);
  7387.  
  7388.       execute_string ("%s\n", string);
  7389.       free (string);
  7390.     }
  7391. }
  7392.  
  7393. int
  7394. cm_macro ()
  7395. {
  7396.   register int i;
  7397.   char *line, *name, *expansion;
  7398.  
  7399.   get_rest_of_line (&line);
  7400.   canon_white (line);
  7401.  
  7402.   for (i = 0; line[i] && !whitespace (line[i]); i++);
  7403.   name = (char *)xmalloc (i);
  7404.   strncpy (name, line, i);
  7405.   name[i] = '\0';
  7406.  
  7407.   while (whitespace (line[i]))
  7408.     i++;
  7409.  
  7410.   add_macro (name, line + i, input_filename, line_number - 1);
  7411.   free (line);
  7412.   free (name);
  7413. }
  7414.  
  7415. int
  7416. cm_unmacro ()
  7417. {
  7418.   register int i;
  7419.   char *line, *name;
  7420.   MACRO_DEF *def;
  7421.  
  7422.   get_rest_of_line (&line);
  7423.   canon_white (line);
  7424.  
  7425.   for (i = 0; line[i] && !whitespace (line[i]); i++);
  7426.   name = (char *)xmalloc (i);
  7427.   strncpy (name, line, i);
  7428.   name[i] = '\0';
  7429.  
  7430.   def = delete_macro (name);
  7431.  
  7432.   if (def)
  7433.     {
  7434.       free (def->filename);
  7435.       free (def->name);
  7436.       free (def->definition);
  7437.       free (def);
  7438.     }
  7439.  
  7440.   free (line);
  7441.   free (name);
  7442. }
  7443. #endif /* HAVE_MACROS */
  7444.  
  7445. /* **************************************************************** */
  7446. /*                                                                  */
  7447. /*                  Looking For Include Files                       */
  7448. /*                                                                  */
  7449. /* **************************************************************** */
  7450.  
  7451. /* Given a string containing units of information separated by colons,
  7452.    return the next one pointed to by INDEX, or NULL if there are no more.
  7453.    Advance INDEX to the character after the colon. */
  7454. char *
  7455. extract_colon_unit (string, index)
  7456.      char *string;
  7457.      int *index;
  7458. {
  7459.   int i, start;
  7460.  
  7461.   i = *index;
  7462.  
  7463.   if (!string || (i >= strlen (string)))
  7464.     return ((char *)NULL);
  7465.  
  7466.   /* Each call to this routine leaves the index pointing at a colon if
  7467.      there is more to the path.  If I is > 0, then increment past the
  7468.      `:'.  If I is 0, then the path has a leading colon.  Trailing colons
  7469.      are handled OK by the `else' part of the if statement; an empty
  7470.      string is returned in that case. */
  7471.   if (i && string[i] == ':')
  7472.     i++;
  7473.  
  7474.   start = i;
  7475.  
  7476.   while (string[i] && string[i] != ':') i++;
  7477.  
  7478.   *index = i;
  7479.  
  7480.   if (i == start)
  7481.     {
  7482.       if (string[i])
  7483.     (*index)++;
  7484.  
  7485.       /* Return "" in the case of a trailing `:'. */
  7486.       return (savestring (""));
  7487.     }
  7488.   else
  7489.     {
  7490.       char *value;
  7491.  
  7492.       value = (char *)xmalloc (1 + (i - start));
  7493.       strncpy (value, &string[start], (i - start));
  7494.       value [i - start] = '\0';
  7495.  
  7496.       return (value);
  7497.     }
  7498. }
  7499.  
  7500. /* Return the full pathname for FILENAME by searching along PATH.
  7501.    When found, return the stat () info for FILENAME in FINFO.
  7502.    If PATH is NULL, only the current directory is searched.
  7503.    If the file could not be found, return a NULL pointer. */
  7504. char *
  7505. get_file_info_in_path (filename, path, finfo)
  7506.      char *filename, *path;
  7507.      struct stat *finfo;
  7508. {
  7509.   char *dir;
  7510.   int result, index = 0;
  7511.  
  7512.   if (path == (char *)NULL)
  7513.     path = ".";
  7514.  
  7515.   /* Handle absolute pathnames. "./foo", "/foo", "../foo". */
  7516.   if (*filename == '/' ||
  7517.       (*filename == '.' &&
  7518.        (filename[1] == '/' ||
  7519.     (filename[1] == '.' && filename[2] == '/'))))
  7520.     {
  7521.       if (stat (filename, finfo) == 0)
  7522.     return (savestring (filename));
  7523.       else
  7524.     return ((char *)NULL);
  7525.     }
  7526.  
  7527.   while (dir = extract_colon_unit (path, &index))
  7528.     {
  7529.       char *fullpath;
  7530.  
  7531.       if (!*dir)
  7532.     {
  7533.       free (dir);
  7534.       dir = savestring (".");
  7535.     }
  7536.  
  7537.       fullpath = (char *)xmalloc (2 + strlen (dir) + strlen (filename));
  7538.       sprintf (fullpath, "%s/%s", dir, filename);
  7539.       free (dir);
  7540.  
  7541.       result = stat (fullpath, finfo);
  7542.  
  7543.       if (result == 0)
  7544.     return (fullpath);
  7545.       else
  7546.     free (fullpath);
  7547.     }
  7548.   return ((char *)NULL);
  7549. }
  7550.